Skip to content

Instantly share code, notes, and snippets.

@jakefolio
Forked from bobmagicii/gist:3409562
Created August 21, 2012 03:11
Show Gist options
  • Save jakefolio/3411185 to your computer and use it in GitHub Desktop.
Save jakefolio/3411185 to your computer and use it in GitHub Desktop.
<?php
class AncientFileFilter extends \FilterIterator {
protected $DaysOld = 7;
public function __construct($dir,$days=null) {
// is the specified directory valid?
if(!is_string($dir) || !is_dir($dir) || !is_readable($dir))
throw new Exception("This '{$dir}' is not valid or readable.");
// construct with an inner filesystem iterator.
parent::__construct(new \FilesystemIterator($dir));
// how old is old in this day of age?
if(is_int($days) || is_float($days))
$this->DaysOld = $days;
return;
}
public function accept() {
$file = $this->current();
// list the file as ancient if it has been stale for more than
// x days.
if($file->getMTime() <= (time()-(86400*$this->DaysOld)))
return true;
// else do not list the file.
else return false;
}
}
// delete files more than 30 days stale.
$diter = new AncientFileFilter('/some/directory', 30);
iterator_apply($diter, 'unlink', $diter->current()->getPathname());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment