How do you auto delete all files under a sub directory after x-time (let say after 24 hours) - without using a cronjob command from server or pl. How can you do this just us
$path = 'folder/subfolder/';
/* foreach (glob($path.'.txt') as $file) { */
foreach (glob($path.'*') as $file) {
if(time() - filectime($file) > 86400){
unlink($file);
}
}
After trying to use these examples, I hit a couple of issues:
I changed the example to use filemtime and fixed the comparison as follows:
<?php
$path = dirname(__FILE__).'/files';
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ((time()-filemtime($path.'/'.$file)) > 86400) { // 86400 = 60*60*24
if (preg_match('/\.txt$/i', $file)) {
unlink($path.'/'.$file);
}
}
}
}
?>