[英]How to clear Laravel storage folder when migrate refresh with seed option
迁移刷新时如何清除 Laravel 存储文件夹? 我想在运行命令php artisan migrate:refresh --seed
时清除storage/app/public
文件夹
我尝试过的:添加(new Filesystem)->deleteDirectory(storage_path('app/public/images'));
在database/seeders/DatabaseSeeder.php
但不工作,当我
我将在这里分享一些片段,您可以将其放入您的database/seeders/DatabaseSeeder.php中,这样它就可以完成所有工作。 只是不要忘记, 默认情况下您可能会有storage/app/public/.gitignore文件,不需要删除。 所以这是一个删除文件和文件的片段,但有例外。
注意:在运行播种机之前运行“ php artisan storage:link ”,以访问存储磁盘中的资源。
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Filesystem\Filesystem;
class DatabaseSeeder extends Seeder
{
public function run()
{
$fs = new Filesystem;
// delete directories
$except_folder_names = [
// folder name (storage/app/public/<folder_name>)
];
$folder_paths = $fs->directories(public_path('storage'));
foreach ($folder_paths as $folder_path) {
$folder_name = last(explode('/', $folder_path));
if (!in_array($folder_name, $except_folder_names)) {
$fs->deleteDirectory($folder_path);
}
}
// delete files
$except_file_names = [
'.gitignore',
// file name (storage/app/public/<file_name>)
];
$file_paths = $fs->files(public_path('storage'));
foreach ($file_paths as $file_path) {
$file_name = last(explode('/', $file_path));
if (!in_array($file_name, $except_file_names)) {
$fs->delete($file_path);
}
}
echo "Uploads successfully deleted!\n";
// OTHER SEEDERS...
}
}
有了这个,你可以从storage/app/public中删除所有的东西(递归的文件和文件夹)
在其他情况下,如果您只想清理目录而不是删除它们,则可以使用以下内容:
$directory = public_path('storage/' . $folder_name);
$file->cleanDirectory($directory);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.