Domain Empire

Need script to delete files older then 2hrs old

NameSilo
Watch
Impact
1
Hi guys

I am running a youtube to mp3 site that has taken off and because of the number of downloads now it is overloading my storage space everyday so I have to manually delete files.

Does any one know of a code that I can set up via php and cron to delete files from a spefic folder once the file is 2hrs old?

Thanks
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Not PHP but bash:

Code:
find [location] -name [file specifications] -type f -mmin +120 -delete

example:
Code:
find /directorytoclean -name *.mp3  -type f -mmin +120 -delete

Something along these lines.
 
1
•••
Quick and dirty but this would work in PHP

Code:
$dir    = 'files/';
$files = scandir($dir, 1);
$twoweeks = (time()-1209600);
foreach($files AS $file) {
if(!($file == '..' OR $file == '.')) {
$file = $dir .$file;
$moddate = filemtime($file);
if($moddate<$twoweeks ) echo $file.' is older than 2 weeks, deleted.<br />'.unlink($file);
}
}

100% should be improved upon
 
1
•••
You may need to use -exec rm {} if -delete not supported

One thing you could do is create a file as part of the cron called "mostrecent" and use that to delete everything since the last time it ran... that way you're not limited to 2 hrs...

find [location] -name [file specifications] -type f -newer mostrecent -delete

or use cname and touch the mostrecent

manpage find is your friend :)
 
Last edited:
1
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back