下面的代码每 60 秒删除文件夹“Images”中的文件,它可以工作,但是当文件夹为空时,它会显示:警告:为 foreach() 提供的参数无效,如果没有文件,如何解决? " 文件夹为空而不是那个警告..
<?php
$expiretime=1;
$tmpFolder="Images/";
$fileTypes="*.*";
foreach (glob($tmpFolder . $fileTypes) as $Filename) {
// Read file creation time
$FileCreationTime = filectime($Filename);
// Calculate file age in seconds
$FileAge = time() - $FileCreationTime;
// Is the file older than the given time span?
if ($FileAge > ($expiretime * 60)){
// Now do something with the olders files...
echo "The file $Filename is older than $expiretime minutes\r\n";
//delete files:
unlink($Filename);
}
}
?>