I am storing files at local storage. So, in /storage/app/public
directory.
I am storing my files in /storage/app/public/userId/images
;
I used php artisan storage:link
, so I can access that files in view, having a shortcut to this folder in /public/storage/userId/images
Inside that path I have 2 images - test.jpg
and test2.jpg
I can't find a response at Laravel documentation, how to delete file test.jpg
from /public/storage/userId/images
I tried in this way :
$path = 'public/' . $id . '/diploma';
$files = Storage::files($path);
return $files;
It returns me :
[
"public/303030/images/test.jpg"
"public/303030/images/test2.jpg"
]
Now, how can I call Storage::delete('test.jpg')
on that array?
One way to delete a file from the public directory in Laravel is to use the Storage facade. To delete a file, you will need to follow the following steps: Step 1: Check to ensure that the folder and file exist. Step 2: Delete the required file.
You could use PHP's unlink() method just as @Khan suggested. But if you want to do it the Laravel way, use the File::delete() method instead. $files = array($file1, $file2); File::delete($files);
Locate the file that you want to delete. Right-click the file, then click Delete on the shortcut menu. Tip: You can also select more than one file to be deleted at the same time. Press and hold the CTRL key as you select multiple files to delete.
There is multiple ways to delete image
//In laravel
File::delete($image);
//for specific directory
File::delete('images/' . 'image1.jpg');
and other way (Simple PHP)
//Simple PHP
unlink(public_path('storage/image/delete'));
and if you want to delete more than 1 images than
Storage::delete(['file1.jpg', 'file2.jpg']);
//or
File::delete($image1, $image2, $image3);
for more detail about Delete File in Laravel
Use Storage::delete(). The delete
method accepts a single filename or an array of files to remove from the disk.
Storage::delete($file_to_delete);
May be you want to do something like-
$files = Storage::files($path);
Storage::delete($files);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With