views:

466

answers:

1

I just got PHP-CLI working on my Windows machine so I could create scripts using PHP. However, I'm trying to write a script to cleanup my Firefox downloads folder of files older than X number of days, but I can't seem to get the filemtime() function working.

Here is the function I wrote:

function deleteOldFiles($dir, $days) {
  $mydir = opendir($dir);
  while(($file = readdir($mydir)) !== false) {
    if($file != "." && $file != ".." && (filemtime($dir.$file) <= time() - ($days * 86400))) {
      //unlink($dir.$file) or DIE("Failed to delete $dir$file<br />");
      echo filemtime($dir.$file);
    }
  }
  closedir($mydir);
}

And upon running this I get the following error for every file:

Warning: filemtime(): stat failed for E:\My Documents\Downloadsphp-5.3.0-nts-Win32-VC9-x86.msi in E:\_scripts\cleanupDownloads\cleanupDownloads.php on line 10

From the research I've done, filemtime() should work in Windows. What am I doing wrong?

+2  A: 

Warning: filemtime(): stat failed for E:\My Documents\Downloadsphp-5.3.0-nts-Win32-VC9-x86.msi in E:\_scripts\cleanupDownloads\cleanupDownloads.php on line 10

You're missing a backslash \ between the directory and the filename. Don't forget to escape it on Windows (\\) or just use the forward slash (/) or PHP's DIRECTORY_SEPARATOR constant.

edit

And have a look at scandir(), glob() or DirectoryIterator.

Philippe Gerber
Yeah, I'm an idiot and caught this later... thanks though!
PHLAK