I did something similar to this a long time ago. It's a terrible idea, but you can have a file which stores the last time your directory was cleared. Each time a user visits a relevant page, check this file in the PHP script (you could also check the modified time). If it is far enough into the past, update the file and run your delete script.
Downsides:
- Not guaranteed to run every 24 hours (maybe you get no visitors one day)
- Gives one user per day a longer wait
- Ugly
As for deleting the files,
function delete_contents( $dirpath ) {
$dir = opendir( $dirpath );
if( $dir ) {
while( ($s = readdir( $dir )) !== false ) {
if( is_dir( $s ) ) {
delete_contents( $s );
rmdir( $s );
} else {
unlink( $s );
}
}
closedir( $dir );
}
}
BE VERY CAREFUL with that. On a crude server setup, delete_contents('/')
will delete every file.