cleanup.php 1.2 KB

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