用php代码删除过期文件
PHP可以删除过期文件吗? 我试着用这样的代码, 但是php会删除文件夹中的所有文件 我的代码:用php代码删除过期文件,php,Php,PHP可以删除过期文件吗? 我试着用这样的代码, 但是php会删除文件夹中的所有文件 我的代码: $dir = 'images/'; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if(is_file($dir."/".$file)) { $file_date = d
$dir = 'images/';
if (is_dir($dir)) { if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if(is_file($dir."/".$file)) {
$file_date = date ("d-m-Y", filemtime($dir."/".$file));
echo $file_date ;
$file_ch_exp_date = strtotime( $file_date);
//echo $file_ch_exp_date ;
echo "<br>";
if(time() > $file_ch_exp_date) {
unlink($dir."/".$file);
}
}
}
closedir($dh);
}
}
$dir='images/';
if(is_dir($dir)){if($dh=opendir($dir)){
while(($file=readdir($dh))!==false){
如果(是_文件($dir./“$file)){
$file_date=date(“d-m-Y”,filemtime($dir./“$file));
echo$file\u日期;
$file\u ch\u exp\u date=strotime($file\u date);
//echo$file\u ch\u exp\u date;
回声“
”;
如果(时间()>$file\u ch\u exp\u日期){
取消链接($dir./“$file);
}
}
}
closedir($dh);
}
}
我的文件夹中有要删除的文件:
1.jpg 2017年6月19日
2.jpg 2017年6月19日
3.jpg 2017年6月19日
我不想删除
ok.jpg 28.06.2017您可以尝试这段代码,它可能会有所帮助
$files = glob(cacheme_directory()."*");
$now = time();
foreach ($files as $file) {
if (is_file($file)) {
if ($now - filemtime($file) >= 60 * 60 * 24 * 2) { // 2 days
unlink($file);
}
}
}
您可以在此处为其参考签出答案filemtime()函数返回上次修改文件内容的时间。因此,它指的是过去的日期,我认为对于所有文件,if station返回true并删除它。time()>$file\u ch\u exp\u date似乎违反直觉。您想删除过期日期之前的文件,因此需要对时间进行反向检查()<$file\u ch\u exp\u date正如Osama已经指出的那样,filemtime返回上次修改文件的时间。如果要检查创建日期,则需要filectime,注意名称中的c而不是m。这意味着创造而不是修改。这回答了你的问题吗?谢谢你的这项it工作,我非常感谢