Just curious
$files = glob(cacheme_directory()."*");
foreach($files as $file)
{
$filemtime=filemtime ($file);
if (time()-$filemtime>= 172800)
{
unlink($file);
}
}
I just want to make sure if the code is correct or not. Thanks.
You should add an
is_file()
check, because PHP normally lists.
and..
, as well as sub-directories that could reside in the the directory you're checking.Also, as this answer suggests, you should replace the pre-calculated seconds with a more expressive notation.
Alternatively you could also use the
DirectoryIterator
, as shown in this answer. In this simple case it doesn't really offer any advantages, but it would be OOP way.Looks correct to me. I'd just suggest you replace
172800
with2*24*60*60
for clarity.Another simplier and more modern way, using FilesystemIterator.
I'm using 'logs' directory as an example.
Main advantage is: DirectoryIterator returns virtual directories "." and ".." in a loop. But FilesystemIterator ignores them.
The easiest way is by using DirectoryIterator:
Hope it help you.