Home  >  Article  >  Backend Development  >  Automatic image cleaner – PHP

Automatic image cleaner – PHP

WBOY
WBOYOriginal
2016-07-25 08:49:501192browse
图片自动清理程序 - PHP
  1. /**
  2. * Picture cleaning scheduler, delete files that have not been accessed in the next two weeks
  3. */
  4. $sRootPath = dirname(__FILE__);
  5. //define(TIME_LINE ,"-7 day"); //删除几天没有访问图片的时间
  6. $dir = $sRootPath .DIRECTORY_SEPARATOR.'upload';
  7. $iTimeLine = strtotime("-7 day");
  8. //$iTimeLine = time();
  9. $sHandDate = date("Ymd");
  10. $sLogDir = dirname(__FILE__).DIRECTORY_SEPARATOR.'Imglog';
  11. $sLog = $sLogDir.DIRECTORY_SEPARATOR.$sHandDate.'.txt';
  12. if(!file_exists($sLogDir)) mkdir($sLogDir, 0777,true);
  13. _clearFile($dir , $iTimeLine, $sLog);
  14. $sEnd = 'AT'."\t" .date("Y-m-d H:i:s")."\t".'EXEC OVER'."\n";
  15. echo $sEnd;
  16. error_log($sEnd, 3, $sLog);
  17. /**
  18. * Clear file operation, pass in the path of the file to be cleared
  19. * @param unknown_type $sPath
  20. */
  21. function _clearFile($sPath, $iTimeLine, $sLog){
  22. if(is_dir($sPath)){
  23. $fp = opendir($sPath);
  24. while(!false == ($fn = readdir($fp))){
  25. if($fn == '.' || $fn =='..') continue;
  26. $sFilePath = $sPath.DIRECTORY_SEPARATOR.$fn;
  27. _clearFile($sFilePath ,$iTimeLine, $sLog);
  28. }
  29. }else{
  30. if($sPath != '.' && $sPath != '..'){ //. ..文件直接跳过,不处理
  31. $iLastView = fileatime($sPath);
  32. if($iLastView < $iTimeLine){
  33. if(@unlink($sPath) === true){
  34. //echo date("Y-m-d H:i:s").'成功删除文件'.$sPath;
  35. //file_put_contents($sLog,'success del file :'.$sPath."\n", FILE_APPEND);
  36. //exit;
  37. $str =date("Y-m-d H:i:s")."\t".'success del file :'.'['.$sPath.']'."\n";
  38. error_log($str, 3, $sLog);
  39. //exit;
  40. }
  41. }
  42. }
  43. }
  44. }
  45. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn