有没有一种方法可以删除特定目录中的所有文件。我试图清理存储\app\backgrounds中我创建的文件夹背景中的所有文件,但在文档中似乎没有删除所有文件的方法。
Storage::delete('backgrounds\*.jpg');
有没有一种方法可以删除特定目录中的所有文件。我试图清理存储\app\backgrounds中我创建的文件夹背景中的所有文件,但在文档中似乎没有删除所有文件的方法。
Storage::delete('backgrounds\*.jpg');
我不确定这是否是解决问题的最佳方法,但我通过打电话解决了我的问题。
use Illuminate\Filesystem\Filesystem;
然后初始化新实例
$file = new Filesystem;
$file->cleanDirectory('storage/app/backgrounds');
适用于 Laravel >= 5.8
use Illuminate\Support\Facades\Storage;
// Get all files in a directory
$files = Storage::allFiles($dir);
// Delete Files
Storage::delete($files);
只需使用它。
File::cleanDirectory($direction);
use File;
或更好的 use Illuminate\Support\Facades\File;
)。
您还可以与存储磁盘一起使用:File::cleanDirectory(Storage::disk('some-disk')->path(''));
。 - Ken$success = Storage::cleanDirectory($directory);
https://laravel.com/api/5.5/Illuminate/Filesystem/Filesystem.html#method_cleanDirectory
此链接是有关如何清理目录的IT技术文档,请详细查看。cleanDirectory
的提示,但它并不能解决调用那些Storage::cleanDirectory
时出现错误的问题,错误信息为League\Flysystem\Filesystem::cleanDirectory
。 - ßiansor Å. ÅlmerolStorage::deleteDirectory('backgrounds');
请记住要包括以下内容:
use Illuminate\Support\Facades\Storage;
Storage
外观清空一个目录,如下所示:Storage::delete(Storage::files('backgrounds'));
$dirs = Storage::directories('backgrounds');
foreach ($dirs as $dir) {
Storage::deleteDirectory($dir);
}
delete()
方法可以接收一个要删除的文件数组,而deleteDirectory()
方法则一次只能删除一个目录(以及其中的内容)。
我认为删除然后重新创建目录并不是一个好主意,因为这可能导致不必要的竞态条件。
我会通过删除整个目录来处理这个问题,因为我不需要它。但是,如果有任何情况下你需要该目录,你只需重新创建即可:
$d = '/myDirectory'
Storage::deleteDirectory($d);
Storage::makeDirectory($d);
//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