Chu*_*ckO 28 php file delete-file
我写这个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);
}
}
}
}
?>
Run Code Online (Sandbox Code Playgroud)
Mik*_*ike 60
<?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);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
您还可以通过在*(通配符)之后添加扩展名来指定文件类型,例如
对于jpg图像使用: glob($dir."*.jpg")
对于txt文件,请使用: glob($dir."*.txt")
对于htm文件,请使用: glob($dir."*.htm")
ssu*_*ube 31
(time()-filectime($path.$file)) < 86400
Run Code Online (Sandbox Code Playgroud)
如果当前时间和文件的更改时间在 86400秒之内,那么......
if (preg_match('/\.pdf$/i', $file)) {
unlink($path.$file);
}
Run Code Online (Sandbox Code Playgroud)
我想这可能是你的问题.将其更改为>或> =,它应该正常工作.
小智 7
<?php
$dir = getcwd()."/temp/";//dir absolute path
$interval = strtotime('-24 hours');//files older than 24hours
foreach (glob($dir."*") as $file)
//delete if older
if (filemtime($file) <= $interval ) unlink($file);?>
Run Code Online (Sandbox Code Playgroud)