Reached's avatar

file_exists method to delete old image

Hi guys,

I have a method that handles image uploads. This method needs to delete the old image, before the new one is uploaded so I dont have to do a lot of manual cleanup.

Im using Laravels file_exists with success a bit further down, in my method, however in this particular case I want to basically just return a string from my javascript, that it should check against to get the old image path.

However it does not delete the old image, from the images folder, what am I doing wrong here?

public function uploadImage(Request $request)
    {
        $oldImage = $request->get('old_image_path');

        if (file_exists(public_path($oldImage))) {
            Storage::delete(public_path($oldImage));
        }
    ... 
} 

When I dd $oldImage i get this:

"/images/marker.svg"
0 likes
3 replies
d3xt3r's avatar

You will have to provider proper driver/disk info. By default (as in config/filesystems.php) default disk is local which points to

'root' => storage_path('app'), 

All paths will be resolved relative to above.

1 like
Reached's avatar

Should it then be?

'media' => [
            'driver' => 'local',
            'root' => storage_path('public'),
        ],
d3xt3r's avatar

storage_path() refers to storage directory, you could set something up like

'media' => [
            'driver' => 'local',
            'root' => public_path('media'),
        ],

I think you might even have to to provision the permissions correctly.

Please or to participate in this conversation.