NoneNameDeveloper's avatar

Doesn't delete files if exists from public folder | Laravel 5.7

I am using Laravel app with Intervention Image. However, if i upload a new image, old images will not be deleted. I'm writing a controller as follows.


//user image
        $image_icon = $request->file('image_icon');
        if($image_icon) {
            \File::delete(public_path() .'/upload/user/'.$user->image_icon.'-b.jpg');
            \File::delete(public_path() .'/upload/user/big'.$user->image_icon.'-s.jpg');
            $tmpFilePath = 'upload/user/';
            $tmpFilePathBig = 'upload/user/big/';
            $hardPath =  str_slug($inputs['full_name'], '-').'-'.md5(time());
            $img = Image::make($image_icon);
            $img1 = Image::make($image_icon);
            $img->fit(270, 225)->save($tmpFilePath.$hardPath.'-b.jpg');
            $img1->fit(360, 360)->save($tmpFilePathBig.$hardPath.'-s.jpg');
            $user->image_icon = $tmpFilePath.$hardPath;
        } 

Now i have doesn't work File::delete method. There is no file upload error. But the old image is not removed. How can i fix this problem? Thanks!
0 likes
3 replies
Snapey's avatar

You are saving new file to upload/teachers but deleting from upload/user ?

NoneNameDeveloper's avatar

@SNAPEY - Oy Sorry, I fixed it, this is not error. All pictures that are stored in the correct user/ directory. The whole code is working correctly but the old uploaded photos are not removed. Automatically saving a new image, but the old ones are not deleted.

Snapey's avatar

The obvious debug step is to dump out the path you are deleting and check there is a file at that location to be deleted.

and pay attention to letter case.

Please or to participate in this conversation.