cleanup.php 995 B

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