Yousef Altaf
Yousef Altaf

Reputation: 2763

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

Upvotes: 1

Views: 121

Answers (1)

Vision Coderz
Vision Coderz

Reputation: 8078

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

Upvotes: 1

Related Questions