Yousef Altaf Yousef Altaf - 1 month ago 9
PHP Question

Laravel : File:delete don't delete the file

I have this function to delete image when the row is deleted

public function destroy($id)
{
$slider = Slider::find($id);
$fileName = $slider->image;
$fullPath = asset('/images/banner/' . $fileName);
//dd($fullPath);
File::delete($fullPath);
$slider->delete();
return Redirect::to('admin/home-slider');
}


but sill returned without the file delete.
what I did wrong here?

Update
for other who may face the same problem

in my case I deleted the
public
from my project before to clean my URL based on this tutorial

Solved by just removing the
asset
from the
$fullPath

and left it just like that
$fullPath = 'images/banner/' . $fileName;


and it's working just fine now

Answer

Use base_path() instead of asset

public function destroy($id)
    {
        $slider = Slider::find($id);
        $fileName = $slider->image;
        $fullPath = base_path().'/public/images/banner/'. $fileName;
        //dd($fullPath);
        File::delete($fullPath);
        $slider->delete();
        return Redirect::to('admin/home-slider');
    }

updated

you can add all images inside the public folder

enter image description here

Comments