cleanup.php 843 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. if (php_sapi_name() != 'cli') { exit; } //do not run from browser
  3. $dir = dirname(__FILE__);
  4. $a_dir = realpath($dir.'/../../archive/');
  5. $list = scandir($a_dir);
  6. $t = time()-(86400*7);
  7. foreach($list as $item) {
  8. if (substr($item,0,1) == '.') { continue; }
  9. $stat = @stat($a_dir.'/'.$item);
  10. if ($stat === false) { error_log('Cron task cannot stat '.$a_dir.'/'.$item); continue; }
  11. if ($stat['mtime'] > $t) { //if the file is older than one week, delete
  12. recursiveDelete($a_dir.'/'.$item);
  13. }
  14. }
  15. /**
  16. * Delete a file or recursively delete a directory
  17. *
  18. * @param string $str Path to file or directory
  19. */
  20. function recursiveDelete($str){
  21. if(is_file($str)){
  22. return @unlink($str);
  23. }
  24. elseif(is_dir($str)){
  25. $scan = glob(rtrim($str,'/').'/*');
  26. foreach($scan as $index=>$path){
  27. recursiveDelete($path);
  28. }
  29. return @rmdir($str);
  30. }
  31. }