namespace service;
class CleanCache {
private $list = array();
private $path;
private $count = 0;
private function getListFiles() {
if(is_dir($this->path)) {
$this->list = array_slice(scandir($this->path), 2);
}
else {
throw new \Exception("The " . $this->path . " is not a directory");
}
}
private function delCacheFile() {
foreach($this->list as $file) {
$filePath = $this->path . "/" . $file;
if(file_exists($filePath)) {
if(unlink($filePath)) {
// echo "delete done " . $filePath . "\r\n";
$this->count++;
}
}
}
}
private function printCount() {
echo "\r\n";
echo "delete files: " . $this->count;
echo "\r\n";
}
public function delCache($_path) {
$this->path = $_path;
$this->getListFiles();
$this->delCacheFile();
// $this->printCount();
return 'done';
}
}
$files = new CleanCache();
echo $files->delCache("cache");
Просто добавляете этот код в крон и выставляете периодичность.













