• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1806
  • Last Modified:

PHP delete files older than 5 days from directory and all sub-directories

Hi,

I need a little bit of help.  I need to write a script that will look through all directories and sub-directories and delete any files that are more than 5 days old.  I can get it to delete files from the top level images directory (root/images/), but I don't know how to get it to check in the sub-diretories.

My structure is as follows:

root > index.php
root/images > some files
root/images/sub1 > some files
root/images/sub1/subsub1 > some files
root/images/sub2 > some files

The code so far is:

<?php
$files = array();
$index = array();
$timeago = strtotime('-5 days');

if ($handle = opendir('images')) {
	clearstatcache();
	while (false !== ($file = readdir($handle))) {
   		if ($file != "." && $file != "..") {
   			$files[] = $file;
			$index[] = filemtime( 'images/'.$file );
   		}
	}
  	closedir($handle);
}
	
asort( $index );
	
foreach($index as $i => $t) {	
	if($t < $timeago) {
		@unlink('images/'.$files[$i]);
	}
}
?>

Open in new window


Please can someone point me in the right direction?

Thank you for your time,

John
0
john-formby
Asked:
john-formby
  • 2
1 Solution
 
Ahmed MerghaniCommented:
Try this:

<?php
function delete_all($directory){
$files = array();
$index = array();
$timeago = strtotime('-5 days');

if ($handle = opendir($directory)) {
	clearstatcache();
	while (false !== ($file = readdir($handle))) {
   		if ($file != "." && $file != "..") {
   			$files[] = $file;
			$index[] = filemtime($directory.'/'.$file );
                        if(is_dir($file)) delete_all($directory.'/'.$file); 
   		}
	}
  	closedir($handle);
}
	
asort( $index );
	
foreach($index as $i => $t) {	
	if($t < $timeago) {
		@unlink('images/'.$files[$i]);
	}
}
}
delete_all('images');
?>

Open in new window

0
 
Roger BaklundCommented:
You need a recursive function for this.

function RemoveFiles($path,$timeago) {
  $files = array();
  $index = array();
  if ($handle = opendir($path)) {
    #clearstatcache();   # not needed, unlink() clears the cache automatically
    while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
        if(is_dir($path.'/'.$file))
          RemoveFiles($path.'/'.$file,$timeago);
        elseif(filemtime( $path.'/'.$file ) < $timeago) {
          unlink($path.'/'.$file);
          echo 'Deleted '.$path.'/'.$file."\n";
        }
      }
    }
    closedir($handle);
  }
}

RemoveFiles('images',strtotime('-5 day'));

Open in new window


I don't see any reason to sort the files before you delete them.

I added a echo statement for debug, remove it when you are confident it works as intended.
0
 
Ahmed MerghaniCommented:
Sorry some modifications:
<?php
function delete_all($directory){
$files = array();
$index = array();
$timeago = strtotime('-5 days');

if ($handle = opendir($directory)) {
	clearstatcache();
	while (false !== ($file = readdir($handle))) {
   		if ($file != "." && $file != "..") {
   			$files[] = $file;
			$index[] = filemtime($directory.'/'.$file );
                        if(is_dir($file)) delete_all($directory.'/'.$file); 
   		}
	}
  	closedir($handle);
}	
foreach($index as $i => $t) {	
	if($t < $timeago) {
		@unlink($directory.'/'.$files[$i]);
	}
}
}
delete_all('images');
?>

Open in new window

0
 
john-formbyAuthor Commented:
I tried all three posts but this was the only one that removed the old files from all sub-directories.  Thanks so much for this solution, it has really helped me out :-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now