PHP automatic image cleanup

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.