gpt4 book ai didi

用于删除早于 24 小时的文件的 php 脚本,删除所有文件

转载 作者:IT王子 更新时间:2023-10-29 01:14:25 24 4
gpt4 key购买 nike

我写了这个 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")

关于用于删除早于 24 小时的文件的 php 脚本,删除所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3126191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com