Personally i would not use glob
as one wrong mistake in the code and you might end up deleting more than just the specified files.. If you hard code this, you should be safe-ish..
Using a cron job is also another idea, but generally shared hosts dont allow them if this is what your using.. But i would recommend a cron job that runs say every hour and deletes all files with a timestamp older than 12/24 hours..
Example Script Using GLOB
<?php
$files = glob("thumbnailsfolder/*");
$now = time();
foreach ($files as $file)
if (is_file($file))
if ($now - filemtime($file) >= 60*60*24*2) // 2 days
unlink($file);
?>
Using DirectoryIterator - My Personal Preference
<?php
foreach (new DirectoryIterator("thumbnailsfolder") as $fileInfo) {
if ($fileInfo->isDot()) {
continue;
}
if (time() - $fileInfo->getCTime() >= 1*24*60*60) {
unlink($fileInfo->getRealPath());
}
}
?>
Examples taken from The correct way to delete all files older than 2 days in PhP