-
-
Save jakefolio/3411185 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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