从 Laravel 本地存储中的特定目录中删除文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48317981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:16:00  来源:igfitidea点击:

Delete file from a specific directory in Laravel local storage

phplaravelfile

提问by priMo-ex3m

I am storing files at local storage. So, in /storage/app/publicdirectory.

我正在本地存储中存储文件。所以,在/storage/app/public目录中。

I am storing my files in /storage/app/public/userId/images;

我将我的文件存储在 /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/imagesInside that path I have 2 images - test.jpgand test2.jpg

我使用了php artisan storage:link,所以我可以在视图中访问该文件,/public/storage/userId/images在该路径内有一个指向该文件夹的快捷方式 ,我有 2 个图像 - test.jpgtest2.jpg

I can't find a response at Laravel documentation, how to delete file test.jpgfrom /public/storage/userId/images

我找不到在Laravel文档的响应,如何删除文件test.jpg/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?

现在,我如何调用 Storage::delete('test.jpg')该数组?

回答by Bilal Ahmed

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)

和其他方式(简单的 PHP)

//Simple PHP
unlink(public_path('storage/image/delete'));

and if you want to delete more than 1 images than

如果您想删除 1 张以上的图像

Storage::delete(['file1.jpg', 'file2.jpg']);
//or
File::delete($image1, $image2, $image3);

for more detail about Delete File in Laravel

有关Laravel 中删除文件的更多详细信息

回答by Sohel0415

Use Storage::delete(). The deletemethod accepts a single filename or an array of files to remove from the disk.

使用Storage::delete()。该delete方法接受要从磁盘中删除的单个文件名或文件数组。

Storage::delete($file_to_delete);

May be you want to do something like-

可能你想做类似的事情——

$files =  Storage::files($path);
Storage::delete($files);