I have a cache folder that stores html files. They are overwritten when needed, but a lot of the time, rarely used pages are cached in there also, that just end up using space (after 5 weeks, the drive was full with over 2.7 million cache files).
Whats the best way to loop thru a directory that contains several hundreds of thousands of files, and remove files that are older than 1 day?
I think you could go about this by looping through the directory with readdir and delete based on the timestamp:
The
if((time() - $filelastmodified) > 24*3600)will select files older than 24 hours (24 hours times 3600 seconds per hour). If you wanted days, it should read for example 7*24*3600 for files older than a week.Also, note that
filemtimereturns the time of last modification of the file, instead of creation date.