34

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);
           }
        }
     }
   }
?>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ChuckO
  • 2,543
  • 6
  • 34
  • 41

7 Answers7

68
<?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")

Typewar
  • 825
  • 1
  • 13
  • 28
Mike
  • 944
  • 8
  • 13
34
(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.

ssube
  • 47,010
  • 7
  • 103
  • 140
8
<?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);?>
cfv1000
  • 453
  • 3
  • 10
8
  1. You want > instead.
  2. Unless you're running on Windows, you want filemtime() instead.
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

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.

B. Martin
  • 1,047
  • 12
  • 18
0

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);
      }
    }
}
}
saefry
  • 1
  • 2
0

$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;
        } 
     }
}
Adam Pery
  • 1,924
  • 22
  • 21