PHP - Create a Clean Cache Script (Simple!)
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I am creating images on the fly using PHP and storing them on a cache folder.
Whenever I request an image from the cache folder, I use the fileExists() function which includes a touch command to update the last access time.

This is my current code:

 * @param string $imageKey
 * @param string $imageExtension
 * @return bool
public static function fileExists($imageKey, $imageExtension){
    $filePath = self::getImagesCacheFolderPath() . $imageKey . ".". $imageExtension;
        //Update access time.
        return true;

    return false;

Please code a cleanCache() function which simply deletes all files in the cache folder that were not "touched" in the last X days, and that have extension "jpg,gif, png"

function cleanCache($pathToCacheFolder, $notAccessedInXDays){

This function will run every day on a cronjob.

  • Please ensure you test this function carefully. I don't want to delete other files by mistake and I am afraid of making a critical mistake. You can run exec() or a php loop. Whatever is most efficient and safe.
  • Ensure that the function has some security involved, such as checking that $pathToCacheFolder is not null, and thus command is not run.
  • Ensure only .jpg, .gif, and .png files are deleted.
awarded to alv-c

Crowdsource coding tasks.

1 Solution

Winning solution

Here is my solution

The code is commented so it's preatty easy to understand what it does, and change behaviour of it.


final version

Hi Alv! Thanks for the fast reply. Question does touch() modify the last access time, last modification time, or both? Thank you! Should I keep using touch or remove it? My question is because I also need to do client side caching and if it also modifies last modified time, it might break it.. Please see related bounty:
georgefountain 3 months ago
Touch changes both access time and last modification time. To only change access time using touch you should call touch like this touch($filePath, date('U', filemtime($filePath)), time()). touch function takes three parameters, first one is file path, second is modified time and third is access time. If third parameter is not passed then second parameter is used to change both modified and access time. But what we do here is set the file modification time to the one it already had (using filemtime($filePath))
alv-c 3 months ago
And I think that touch call also solves your other bounty
alv-c 3 months ago
Hi Alv. I want to award you the bounty but your code is not working as expected. Found /cache/5ed182e02f895a3e9317430efec006a7962bf35c-350x182-YNY-0.png 2017-01-12 20:53:12 ext:png ;diff:0 For example, in this case last modification date is "2017-01-12 20:53:12 " and it says difference to NOW is 0.
georgefountain 2 months ago
There is some kind of bug. See more results: Found /cache/5ed182e02f895a3e9317430efec006a7962bf35c-350x182-YNY-0.png 2017-01-12 20:53:12 ext:png ;diff:0 <-- Difference 0??!!! Found /cache/image12diff2a.png 2017-01-18 11:42:43 ext:png ;diff:26 <-- Difference 26 days is correct.
georgefountain 2 months ago
Updated solution. Now it works correctly. My bad, previous version used $diff->d to check for days difference. Previous version calculates difference in days but separates days from months and years. let's say an image was 1 month and 2 days old. Then $diff->d returns 2. Now with $diff->format("%a") it returns the correct difference in days. As I said, I updated the solution with working code
alv-c 2 months ago
%a or just a?
georgefountain 2 months ago
Use %a . Just a will literally print a
alv-c 2 months ago
Thank you!!!
georgefountain 2 months ago
You're welcome, george!
alv-c 2 months ago
View Timeline