This article mainly introduces PHP's automatic image cleanup method, which can clear images that are not accessed within a fixed date. It has some reference value. For more information, see
This article mainly introduces PHP's automatic image cleanup method, which can clear images that are not accessed within a fixed date. It has some reference value. For more information, see
This example describes how to automatically clean up images in PHP. Share it with you for your reference. The specific implementation method is as follows:
<? Php/*** image cleanup scheduler, delete files not accessed in the next two weeks */$ sRootPath = dirname (_ FILE _); // define (TIME_LINE, "-7 day"); // The time when the image is not accessed for several days after deletion $ dir = $ sRootPath. DIRECTORY_SEPARATOR. 'upload'; $ iTimeLine = strtotime ("-7 day"); // $ iTimeLine = time (); $ sHandDate = date ("Ymd "); $ sLogDir = dirname (_ FILE __). DIRECTORY_SEPARATOR. 'imglog'; $ sLog = $slogdir.directory_separator.mongoshanddate.'.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);/*** clear the file operation, input the path * @ param unknown_type $ sPath */function _ clearFile ($ sPath, $ iTimeLine, $ sLog) {if (is_dir ($ sPath )) {$ 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! = '.. '){//... The file is skipped directly and $ iLastView = fileatime ($ sPath) is not processed; if ($ iLastView <$ iTimeLine) {if (@ unlink ($ sPath) === true) {// echo date ("Y-m-d H: I: s "). 'File deleted successfully '. $ 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 php programming.