Ovais Tariq
Ovais Tariq

Reputation: 49

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

Upvotes: 0

Views: 727

Answers (2)

nayeemdev
nayeemdev

Reputation: 1241

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;

Upvotes: 4

Etin
Etin

Reputation: 365

You will need to use the full path to the file in order to delete it.

https://laravel.com/docs/5.8/filesystem#deleting-files

Try using the full path in Storage::disk('uploads')->delete($post->image);

Upvotes: 1

Related Questions