如何删除我在Laravel的公用文件夹中创建的临时文件夹中的文件



我在公共目录中创建了一个名为temp的文件夹。我需要写一个函数来清理日常临时文件夹。如何删除文件夹中的文件?

在app/Concole/Commands/ClearTemp.php上执行名为ClearTemp的自定义手工命令的第一步。ClearTemp.php内容:

<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
use IlluminateSupportFacadesStorage;
class ClearTemp extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'clear_temp';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Clear all file inside temp folder';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
//
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->cleanDirectory('temp');
}
private function cleanDirectory($path, $recursive = false)
{
$storage = Storage::disk('public');
foreach ($storage->files($path, $recursive) as $file) {
$storage->delete($file);
}
}
}

第二步定义你的工作频率时间。在这种情况下,将每天在app/Console/kernel.php上kernel.php内容:

<?php
namespace AppConsole;
use IlluminateConsoleSchedulingSchedule;
use IlluminateFoundationConsoleKernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
// EnsureQueueListenerIsRunning::class
//
];
/**
* Define the application's command schedule.
*
* @param  IlluminateConsoleSchedulingSchedule  $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('clear_temp')->daily();
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__ . '/Commands');
require base_path('routes/console.php');
}
}

最后检查您的工匠命令:

php artisan clear_temp

并确保激活您的系统cron作业,以检查运行函数的确切时间。

最新更新