spaceba's avatar

How to removed image file from storage folder

I have a function in my controller that upload file in my storage folder whenever user upload image in a vue-editor.

public function upload(Request $request)
    {
        $request->file('image')->store('public/images');
        $name = $request->file('image')->hashName();

        if ($request->hasFile('image')) {
            return response()->json(['url'=>'/storage/images/' . $name]);
        }
    }

and now I need to delete the image whenever the image is being deleted from vue-editor, but I am not sure why it didn't work.

public function removeUploaded(Request $request)
    {
        $imagePath = $request->image;
        $slice = Str::after($imagePath, "/storage/images")

        if(Storage::disk('editor')->has($slice)){
             Storage::delete([$slice]);
             return response(['status' => 'Your Message Sent Successfully !']);
         }
    }

This is my disks for editor in filesystems.php:-

'editor' => [
            'driver' => 'local',
            'root' => storage_path('app/public/images/'),
            'visibility' => 'public',
        ]

Can anyone explain to me? Thank you and i appreciate your help

0 likes
2 replies
SilenceBringer's avatar
Level 55

@spaceba possible just forgot to set disk on delete?

Storage::disk('editor')->delete([$slice]);
spaceba's avatar

Yes, you're right. My mistake. Thanks! :)

Please or to participate in this conversation.