I am trying to use this simple bit of code to iterate through the "export" folder and delete files older than 24 hours:
if ($handle = opendir("/home/username/public_html/en/graphs/export")) {
while (false !== ($file = readdir($handle))) {
$filelastmodified = filemtime($file);
if((time() - $filelastmodified) > 24*3600)
{
unlink($file);
}
}
closedir($handle);
}
Some notes:
1) I do realize there are similar questions, but the solutions suggested there don't seem to work for me. 2) The absolute path to the directory is correct (tested) 3) The directory has 777 permissions. The files in it don't, but I tested with some files with 777 permissions and the same errors happened. So it doesn't seem to be a permission issue. 4) The file that contains this code is in a different directory (it's a cron job, I like to keep them together in a separate directory)
This is the error that appears (for each file in the directory):
Warning: filemtime() [function.filemtime]: stat failed for countries_rjRp9.png in /home/username/public_html/path-to-crons/crons/exports.php on line 12
Warning: unlink(countries_rjRp9.png) [function.unlink]: No such file or directory in /home/username/public_html/path-to-crons/crons/exports.php on line 16
In this example, countries_rjRp9.png
is one of the files that should be unlinked from the export
directory.
What's going on here?