Return to Snippet

Revision: 12701
at March 25, 2009 10:11 by fackz


Initial Code
<?php
 
$dir = '/path/to/dir';
if ($handle = opendir($dir)) {
  /* This is the correct way to loop over the directory. */
  while (false !== ($file = readdir($handle))) {
    if ($file[0] == '.' || is_dir("$dir/$file")) {
       // ignore hidden files and directories
       continue;
    }
    if ((time() - filemtime($file)) > ($days *86400)) { //7 days
      unlink("$dir/$file");
    }
  }
  closedir($handle);
}

Initial URL

                                

Initial Description
delete all files in a directory that are more than 7 days old.

Initial Title
Script to delete files in a directory (X days old)

Initial Tags
files

Initial Language
PHP