php脚本删除超过24小时的文件,删除所有文件

发布于 2024-09-07 00:30:28 字数 380 浏览 5 评论 0原文

我编写了这个 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);
           }
        }
     }
   }
?>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

_蜘蛛 2024-09-14 00:30:28
<?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")

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

?>

You can also specify file type by adding an extension after the * (wildcard) eg

For jpg images use: glob($dir."*.jpg")

For txt files use: glob($dir."*.txt")

For htm files use: glob($dir."*.htm")

花辞树 2024-09-14 00:30:28
(time()-filectime($path.$file)) < 86400

如果当前时间和文件的更改时间彼此相差 86400 秒内,那么...

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

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

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

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.

╰ゝ天使的微笑 2024-09-14 00:30:28
  1. 您需要 > 来代替。
  2. 除非您在 Windows 上运行,否则您需要使用 filemtime()
  1. You want > instead.
  2. Unless you're running on Windows, you want filemtime() instead.
多彩岁月 2024-09-14 00:30:28
<?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);?>
<?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);?>
起风了 2024-09-14 00:30:28

我测试了两种方法来比较速度。选项 A 反复快了 50% 左右。我在一个包含大约 6000 个文件的文件夹上运行了这个。

选项A

$path='cache/';
$cache_max_age=86400; # 24h
if($handle=opendir($path)){
    while($file=readdir($handle)){
        if(substr($file,-6)=='.cache'){
            $filectime=filectime($path.$file);
            if($filectime and $filectime+$cache_max_age<time()){
                unlink($path.'/'.$file);
            }
        }
    }
}

选项B

$path='cache/';
$cache_max_age= 86400; # 24h
foreach(glob($path."*.cache") as $file){
    $filectime=filectime($file);
    if($filectime and $filectime+$cache_max_age<time()){
        unlink($file);
    }
}

它还检查是否返回文件创建时间。在某些系统上,返回创建时间存在问题。因此,我想确保如果系统不返回时间戳,它不会删除所有文件。

I tested two methods to compare the speed. Option A was repeatedly about 50% faster. I ran this on a folder with about 6000 files.

Option A

$path='cache/';
$cache_max_age=86400; # 24h
if($handle=opendir($path)){
    while($file=readdir($handle)){
        if(substr($file,-6)=='.cache'){
            $filectime=filectime($path.$file);
            if($filectime and $filectime+$cache_max_age<time()){
                unlink($path.'/'.$file);
            }
        }
    }
}

Option B

$path='cache/';
$cache_max_age= 86400; # 24h
foreach(glob($path."*.cache") as $file){
    $filectime=filectime($file);
    if($filectime and $filectime+$cache_max_age<time()){
        unlink($file);
    }
}

It also checks whether a file creation time was returned. On some systems, there are problems with returning the creation time. Therefore I wanted to make sure that it does not delete all files if the system does not return a timestamp.

回首观望 2024-09-14 00:30:28

工作正常

$path = dirname(__FILE__);
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
$timer = 300;
$filetime = filectime($file)+$timer;
$time = time();
$count = $time-$filetime;
    if($count >= 0) {
      if (preg_match('/\.png$/i', $file)) {
        unlink($path.'/'.$file);
      }
    }
}
}

working fine

$path = dirname(__FILE__);
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
$timer = 300;
$filetime = filectime($file)+$timer;
$time = time();
$count = $time-$filetime;
    if($count >= 0) {
      if (preg_match('/\.png$/i', $file)) {
        unlink($path.'/'.$file);
      }
    }
}
}
执笏见 2024-09-14 00:30:28
$path = '/cache/';
// 86400 = 1day

if ($handle = opendir($path)) {
     while (false !== ($file = readdir($handle))) {
        if ( (integer)(time()-filemtime($path.$file)) > 86400 && $file !== '.' && $file !== '..') {
                unlink($path.$file);
                echo "\r\n the file deleted successfully: " . $path.$file;
        } 
     }
}

$path = '/cache/';
// 86400 = 1day

if ($handle = opendir($path)) {
     while (false !== ($file = readdir($handle))) {
        if ( (integer)(time()-filemtime($path.$file)) > 86400 && $file !== '.' && $file !== '..') {
                unlink($path.$file);
                echo "\r\n the file deleted successfully: " . $path.$file;
        } 
     }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文