tags:

views:

52

answers:

2

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);
           }
        }
     }
   }
?>
+3  A: 
  1. You want > instead.
  2. Unless you're running on Windows, you want filemtime() instead.
Ignacio Vazquez-Abrams
+2  A: 
(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.

peachykeen