Automatic image cleanup program-PHP
Automatic image cleanup program-PHP
- /**
- * The Image cleanup scheduler deletes files that are not accessed for the next two weeks.
- */
- $ SRootPath = dirname (_ FILE __);
- // Define (TIME_LINE, "-7 day"); // delete the time when the image is not accessed for several 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.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 );
- /**
- * To clear a file, enter the path of the file to be cleared.
- * @ 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, not processed
- $ ILastView = fileatime ($ sPath );
- 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;
- }
- }
- }
- }
- }
- ?>
|