我编写了这个 php 脚本来删除超过 24 小时的旧文件,
但它删除了所有文件,包括较新的文件:
<?php
$path = 'ftmp/';
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ((time()-filectime($path.$file)) < 86400) {
if (preg_match('/\.pdf$/i', $file)) {
unlink($path.$file);
}
}
}
}
?>
<?php
/** define the directory **/
$dir = "images/temp/";
/*** cycle through all files in the directory ***/
foreach (glob($dir."*") as $file) {
/*** if file is 24 hours (86400 seconds) old then delete it ***/
if(time() - filectime($file) > 86400){
unlink($file);
}
}
?>
您还可以通过在 *(通配符)后添加扩展名来指定文件类型,例如
对于 jpg 图像使用:glob($dir."*.jpg")
对于 txt 文件使用:glob($dir."*.txt")
对于 htm 文件使用:glob($dir."*.htm")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)