php脚本删除超过24小时的文件,删除所有文件 [英] php script to delete files older than 24 hrs, deletes all files

查看:156
本文介绍了php脚本删除超过24小时的文件,删除所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个php脚本来删除超过24小时的旧文件,
,但它删除了所有的文件,包括较新的文件:

I wrote this php script to delete old files older than 24 hrs, but it deleted all the files including newer ones:

<?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);
           }
        }
     }
   }
?>


推荐答案

(time()-filectime($path.$file)) < 86400

如果当前时间和文件的更改时间在 86400秒内如果(preg_match('/ \.pdf $ / i',$ file)),则可以使用另一个...

If the current time and file's changed time are within 86400 seconds of each other, then...

 if (preg_match('/\.pdf$/i', $file)) {
     unlink($path.$file);
 }

我认为这可能是你的问题。将其更改为>或> =,它应该正常工作。

I think that may be your problem. Change it to > or >= and it should work correctly.

这篇关于php脚本删除超过24小时的文件,删除所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆