Laravel File Storage 删除目录中的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47007356/
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 16:52:52  来源:igfitidea点击:

Laravel File Storage delete all files in directory

phplaravellaravel-5.5file-storage

提问by ?iansor ?. ?lmerol

Is there a way to delete all files in specific directory. I'm trying to clear all my files in my created folder backgrounds in storage\app\backgrounds but in docsseems no method for delete all.

有没有办法删除特定目录中的所有文件。我正在尝试清除我在 storage\app\backgrounds 中创建的文件夹背景中的所有文件,但在文档中似乎没有删除所有文件的方法。

Storage::delete('backgrounds\*.jpg');

回答by ?iansor ?. ?lmerol

I don't think if this is the best way to solve this. But I solved mine calling

我不认为这是否是解决此问题的最佳方法。但我解决了我的电话

use Illuminate\Filesystem\Filesystem;

Then initiate new instance

然后启动新实例

$file = new Filesystem;
$file->cleanDirectory('storage/app/backgrounds');

回答by Patrick Chidera Duruamadi

    use Illuminate\Support\Facades\Storage;

    // Get all files in a directory
    $files =   Storage::allFiles($dir);

    // Delete Files
    Storage::delete($files);

回答by Michael Bausano

You can use Filesystem method cleanDirectory

您可以使用文件系统方法 cleanDirectory

$success = Storage::cleanDirectory($directory);

Please see documentation for more information:

请参阅文档了解更多信息:

https://laravel.com/api/5.5/Illuminate/Filesystem/Filesystem.html#method_cleanDirectory

https://laravel.com/api/5.5/Illuminate/Filesystem/Filesystem.html#method_cleanDirectory

回答by cespon

In Laravel 5.8you can use:

Laravel 5.8 中,您可以使用:

Storage::deleteDirectory('backgrounds');

Remember to include:

记得包括:

use Illuminate\Support\Facades\Storage;

回答by Soulriser

In Laravel 5.7you can empty a directory using the Storagefacade like so:

Laravel 5.7 中,您可以使用StorageFacade清空目录,如下所示:

Storage::delete(Storage::files('backgrounds'));

$dirs = Storage::directories('backgrounds');

foreach ($dirs as $dir) {
    Storage::deleteDirectory($dir);
}

The delete()method can receive an array of files to delete, while deleteDirectory()deletes one directory (and its contents) at a time.

delete()方法可以接收要删除的文件数组,同时一次deleteDirectory()删除一个目录(及其内容)。

I don't think it's a good idea to delete and then re-create the directory as that can lead to unwanted race conditions.

我认为删除然后重新创建目录不是一个好主意,因为这会导致不需要的竞争条件。

回答by Amir Kaftari

Just use it.

就用它。

 File::cleanDirectory($direction);

回答by PriyankMotivaras

//You can use Illuminate\Filesystem\Filesystem and it's method cleanDirectory('path_to_directory).
For Example:
    $FolderToDelete = base_path('path_to_your_directory');
    $fs = new \Illuminate\Filesystem\Filesystem;
    $fs->cleanDirectory($FolderToDelete);   
    //For Delete All Files From  Given Directory.
    $succes = rmdir($FolderToDelete);
    //For Delete Directory
    //This Method Works for me

#Laravel 
#FileManager
#CleanDirectory

回答by brnd0

I'm handling this by deleting the whole directory as I don't need it. But if, for any case, you need the directory you should be good by just recreating it:

我通过删除整个目录来处理这个问题,因为我不需要它。但是,在任何情况下,如果您需要目录,只需重新创建它就可以了:

$d = '/myDirectory'
Storage::deleteDirectory($d);
Storage::makeDirectory($d);