Delete file Laravel

Asked

Viewed 2,985 times

2

When trying to delete an image from a specific directory I get the message that the file does not exist, but consulting the path that the code is running I see that the file does exist, and the path string is 100% correct.

I even considered permission but the code itself is who creates the folder, saves the file inside and reads, only it does not erase...

Storage::delete(public_path('uploads/'.$asset->contents->belongs_to.'/'.$asset->name));

File not found at path: var/www/xpto-digitaldev/public/uploads/2/217dade2ab7db91d12f1bca7b0cd4c82.png

Saving the file from upload;

if ($request->hasFile('image')) {
   $extension = $image->getClientOriginalExtension();
   $newFileName = md5(rand(0,9999)).'.'.$extension;
   $image->move(public_path('uploads/'.$clientDirectory), $newFileName);
}
  • No need to close parentheses in Storage::delete(public_path('uploads/'.$asset->contents->belongs_to.'/'.$asset->name);? And if you try unlink(public_path('uploads/'.$asset->contents->belongs_to.'/'.$asset->name));

  • @Miguel no, the function public_path() just wait 1 parameter, if I had forgotten to close the parentheses I would get a syntax error.

  • Yeah, but it doesn’t look like you closed the delete(...

  • I already got @Miguel

  • If you do with unlink ? as I told you above

  • with unlink worked @Miguel, post your response so I can approve, I will study this case also to see why the delete Laravel’s unlink using did not work.

  • Maybe add something (unwanted in this case) to the path of the file to delete. I usually do it with unlink, ok I’ll post the answer. Obgado

Show 2 more comments

3 answers

2


With Storage::delete(...), never tried, always use the native php function unlink, try the following:

unlink(public_path('uploads/'.$asset->contents->belongs_to.'/'.$asset->name));

1

You can use the File.

File::delete('img/imagem.png');

Don’t forget the:

use File;

File may be the same as Storage.

0

I’m using the Storage so and giving good.

Storage::delete(["images/folder/banners/.ext file}"]);

put an array as parameter for the files you want to delete

Browser other questions tagged

You are not signed in. Login or sign up in order to post.