I have a 'cache' folder on my webserver where .html files are stored, the file structure is as follows:
cache > user@gmail.com > several .html files.
I need a PHP script that will browse all the subdirectors within the user folders and delete files older than 3 months.
I have this script so far:
$DIR = '/cache/';
if ($handle = opendir($DIR)) {
while (false !== ($file = readdir($handle))) {
if ( filemtime($DIR.$file) <= time()-60*60*24*120 ) { //120 days?
unlink($DIR.$file);
}
}
closedir($handle);
}
But it does not handle sub-directories. It is also giving me errors because it is trying to unlink directorys..
Update: trying to add a count feature:
<?php
rrmdir(/www/deletecontentsofthisfolder/);
echo $count . ' files deleted!';
function rrmdir($dir,$count=0)
{
if (is_dir($dir))
{
$objects = scandir($dir);
foreach ($objects as $object)
{
if ($object != "." && $object != "..")
{
if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object,$count);
if (filemtime($dir."/".$object) <= time()-60*60*24*120) @unlink($dir."/".$object); count++
}
}
reset($objects);
//rmdir($dir);
}
return $count;
}