Search code examples
phpsqllaravelblogs

Unable to delete image from the drive in laravel


I am unable to delete the file (uploaded by the user) on calling the destroy method in the post controller.

I am able to upload the file to my drive that is root/public/uploads/attachment

this is my filesystem

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

This is what i have tried in my controller

 public function destroy(Post $post)
{
    $post=Post::find($post->id);
    Storage::disk('uploads')->delete($post->image);
    $post->categories()->detach();
    $post->tags()->detach();
    $post->delete();
    return redirect('admin/post')->with('message','Deleted Sucessfully');
}

I also have tried

unlink(public_path().'/uploads/'.$post->image);

But both actions gives the same results the post gets deleted but when i physically check the attachments folder the image is still present there


Solution

  • If you specify the Disk Then try it(Full Path of Image)..

    Storage::disk('uploads')->delete(public_path().'/uploads/'.$post->image);
    

    Or

    Storage::delete($post->image);
    

    And Must use

    use Illuminate\Support\Facades\Storage;