A few days ago, before the hacking malarky, I needed a script that I could run via Cron to delete the contents of a folder that were older than a week from a certain day. For example; Let’s say it’s Saturday, I want the script to delete all files older than Saturday… Get what I [...]

A few days ago, before the hacking malarky, I needed a script that I could run via Cron to delete the contents of a folder that were older than a week from a certain day. For example; Let’s say it’s Saturday, I want the script to delete all files older than Saturday… Get what I mean. Right. Let’s continue.

It’s actually fairly simple. Only when you know how. :lol: It’s quite a short script too. I’ll write the script first & then explain it:

<?php
$files = array();
$index = array();
$yesterday = strtotime('yesterday');

if ($handle = opendir('relative/path/to/dir')) {
	clearstatcache();
	while (false !== ($file = readdir($handle))) {
   		if ($file != "." && $file != "..") {
   			$files[] = $file;
			$index[] = filemtime( 'relative/path/to/dir/'.$file );
   		}
	}
  	closedir($handle);
}

asort( $index );

foreach($index as $i => $t) {

	if($t < $yesterday) {
		@unlink('relative/path/to/dir/'.$files[$i]);
	}

}
?>

First we set up two arrays & then the timecode for yesterday using strtotime(). Next we open the directory, surrounding it with an if just in case. The path is relative from the location of the script, if you are using it as a cron script I recommend placing it in your home directory or somewhere not accessable from the internet.

We next run clearstatcache() just in case, we don’t want to be trying to delete files that don’t exist. Next we do the file loop. We also use an if to ignore the . & ... We then place every file into the $file array & then get the modified date from the file into the $index array. We then close the handle.

Now onto the sorting. We sort the dates using asort() this keeps key association & sorts the array. Then we run the array through a foreach(). After the sort we can grab the file that matches the time using the times index (since we used asort() to keep association). To delete the correct files we check to see if $t (timecode) is less than yesterday’s timecode. If it is we unlink it (php’s delete function).

If you copy & paste the code please remember to change the file paths. Remember as I said they a relative from the script. Setting the script to run via Cron can be different for different servers but here is the steps I take. This is via SSH:

crontab -e
0 0 * * 6 /full/nix/path/to/php/binary -q /full/nix/path/to/php/script.php

If it opens in nano then use Ctrl+O to write out (save) then Ctrl+X to exit. Remember these are just for my host your may be different. You can always try there should be any harm, but I’m not responsible for any damage caused if it doesn’t work.

I hope that this is of some use to someone. If really came in handy for me & I always keep in in my little box of tricks, even if you don’t want to delete anything it can be used to sort arrays of files by their date modified & can easily be changed to date created.