<?php
namespace app\common\command;
use http\Exception\InvalidArgumentException;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\exception\ErrorException;
class FacialVerificationFileCleanup extends Command
{
protected function configure()
{
$this->setName('fileCleanup')
->addArgument('name', Argument::OPTIONAL, "your name")
->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
->setDescription('Say Hello');
}
protected function execute(Input $input, Output $output)
{
$output->writeln("Hello,thinkphp" . '! 开始清理文件。');
$targetDir = app()->getRootPath() . '/public/uploads/sign/';
try {
$this->deleteDirectory($targetDir);
} catch (ErrorException $e) {
$output->writeln("Error deleting $targetDir: " . $e->getMessage());
}
$output->writeln("Hello,thinkphp" . '! 人脸核验文件清理完毕。时间:' . date('Y-m-d H:i:s'));
}
function deleteDirectory($dir)
{
if (!is_dir($dir)) {
throw new InvalidArgumentException("$dir must be a directory");
}
if (substr($dir, strlen($dir) - 1, 1) != '/') {
$dir .= '/';
}
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $file) {
if ($file->isDir()) {
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
}
}
return [
'app\common\command\FacialVerificationFileCleanup',
];
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/ebec084f31bd478dbc2c284b1ad36f48.png)