我试图删除文件夹中超过x小时的所有文件(当前示例为一小时)。但是它不尊重条件,脚本删除文件夹中的所有文件,不管它们是超过1小时还是小于1小时。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
function destroy($dir) {
$mydir = opendir($dir);
while($file = readdir($mydir)) {
if($file != "." && $file != "..") {
chmod($dir.$file, 0777);
if(is_dir($dir.$file)) {
chdir('.');
while($dir.$file) {
if(date("U",filectime($file)) >= time() - 3600) {
unlink($dir.$file);
echo $file.' had been deleted';
}
}
} else
unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
}
}
closedir($mydir);
}
$docroot = $_SERVER['DOCUMENT_ROOT'];
$docroot = $docroot.'/scrapers/thumbnails/';
destroy($docroot);
?>
try:
function destroy($dir) {
$mydir = opendir($dir);
while($file = readdir($mydir)) {
if($file != "." && $file != "..") {
chmod($dir.$file, 0777);
if(is_dir($dir.$file)) {
chdir('.');
while($dir.$file) {
if((time() - filemtime($file)) > 3600) {
unlink($dir.$file);
echo $file.' had been deleted';
}
}
} else
if((time() - filemtime($file)) > 3600) {
unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
}
}
}
closedir($mydir);
}