function del_cache($dir, $i = '') { //先删除目录下的文件: $dh = opendir($dir); if (!defined('CLEAR_API_CACHE_TIME') || CLEAR_API_CACHE_TIME <= 1) { $day_num = 2; } else { $day_num = CLEAR_API_CACHE_TIME; } //清除缓存文件更新时间小于当前时间减去指定天数文件 $before_time = $day_num * 86400; $i += 1; while ($file = readdir($dh)) { $clear_time = TIMENOW - $before_time; if ($file != "." && $file != "..") { $fullpath = $dir . "/" . $file; //file_put_contents('2.txt',$fullpath.'+++'.$i.'*', FILE_APPEND); if (!is_dir($fullpath)) { $update_time = ''; $update_time = filemtime($fullpath); if (!$update_time) { $update_time = filectime($fullpath); } //文件更新时间小于设定时间才删除 if ($update_time < $clear_time && $file != 'index.html') { unlink($fullpath); } } else { //file_put_contents('3.txt', $fullpath.$i."\n",FILE_APPEND); $this->del_cache($fullpath, $i); } } } closedir($dh); //file_put_contents('4.txt', $dir.$i."\n",FILE_APPEND); //删除当前文件夹: if (isEmptyDir($dir) && $dir !== CACHE_DIR) { rmdir($dir); } }
public function isEmptyDir($folder) { if (!is_dir($folder)) { return array(); } $recursif = false; $files = array(); $dir = opendir($folder); while ($file = readdir($dir)) { if ($file == '.' || $file == '..') { continue; } if (is_dir($folder . '/' . $file)) { $files[] = $folder . '/' . $file; if ($recursif == true) { $files = array_merge($files, isEmptyDir($folder . '/' . $file)); } } else { $files[] = $folder . '/' . $file; } } closedir($dir); if (count($files) <= 0) { return true; } return false; }