jingcai-php/app/Console/Commands/AutoRemoveLotteryFile.php

81 lines
1.9 KiB
PHP
Executable File

<?php
namespace App\Console\Commands;
use App\Enums\ClientIOSType;
use App\Enums\MaterialScene;
use App\Model\Apps;
use App\Model\Material;
use App\Utils\Helps;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class AutoRemoveLotteryFile extends Command
{
/**
* 这个就是命令名称
*/
protected $signature = 'AutoRemoveLotteryFile';
/**
* 命令的说明描述
* @var string
*/
protected $description = '删除两个月前的lottery图片';
/**
* 创建命令的构造方法。
* @param string $words 传入的字符参数
* @return void
*/
public function __construct()
{
parent::__construct();
}
public function handle()
{
$month = date('Ym', strtotime('-2 months'));
$lotteryPath = MaterialScene::getDiskPathForMonth(MaterialScene::LOTTERY, $month);
$path = public_path($lotteryPath);
$fileExist = file_exists($path);
if (!$fileExist) {
return;
}
if (!is_dir($path)) {
return;
}
try {
$this->rrmdir($path);
} catch (\Exception $e) {
Log::error('AutoRemoveLotteryFile remove dir error', [
'path'=> $path,
'error' => $e->getMessage()
]);
}
}
function rrmdir($src) {
$dir = opendir($src);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
$full = $src . '/' . $file;
if ( is_dir($full) ) {
$this->rrmdir($full);
}
else {
Log::info('AutoRemoveLotteryFile remove file:' . $full);
unlink($full);
}
}
}
closedir($dir);
rmdir($src);
Log::info('AutoRemoveLotteryFile remove dir:' . $src);
}
}