The example in this article describes the way in which PHP implements automatic image cleanup. Share to everyone for your reference. The implementation method is as follows:
<?php/** * Image cleanup program, delete files not accessed under two weeks * * * * $sRootPath = dirname (__file__);
Define (Time_line, "-7 Day"); Delete the time when the picture was not accessed for a few days $dir = $sRootPath.
Directory_separator. ' Upload ';
$iTimeLine = Strtotime ("-7 Day");
$iTimeLine = time ();
$sHandDate = Date ("Ymd"); $sLogDir = DirName (__file__).
Directory_separator. ' Imglog '; $sLog = $sLogDir. Directory_separator. $sHandDate.
TXT ';
if (!file_exists ($sLogDir)) mkdir ($sLogDir, 0777,true);
_clearfile ($dir, $iTimeLine, $sLog); $sEnd = ' at '. \\t ". Date (" Y-m-d h:i:s ")." \\t "." EXEC over '. '
\\n ";
Echo $sEnd;
Error_log ($sEnd, 3, $sLog); /** * Clears the file operation, passing in the path where the file needs to be purged * @param unknown_type $sPath/function _clearfile ($sPath, $iTimeLine, $sLog) {if Is_dir ($sP
Ath)) {$fp = Opendir ($sPath);
while (!false = = ($fn = Readdir ($fp))) {if ($fn = = '. ' | | $fn = = ' ... ') continue;
$sFilePath = $sPath. Directory_separator. $fn;
_clearfile ($sFilePath, $iTimeLine, $sLog); }else{if ($sPath!= '. ' && $sPath!= ' ... ') {
//. .. Files are skipped directly, not processed $iLastView = FileAtime ($sPath); if ($iLastView < $iTimeLine) {if (@unlink ($sPath) = = True) {//echo date ("y-m-d h:i:s"). '
Successfully deleted file '. $sPath; File_put_contents ($sLog, ' Success del file: '. $sPath.
\\n ", file_append);
Exit $str =date ("y-m-d h:i:s"). " \\t "." Success del file: '. ' ['. $sPath. '] '. '
\\n ";
Error_log ($STR, 3, $sLog);
Exit
}}}}?>
I hope this article will help you with your PHP programming.