views:

190

answers:

4

I have a cache folder that stores html files. They are overwritten when needed, but a lot of the time, rarely used pages are cached in there also, that just end up using space (after 5 weeks, the drive was full with over 2.7 million cache files).

Whats the best way to loop thru a directory that contains several hundreds of thousands of files, and remove files that are older than 1 day?

A: 

Maybe you can use database ?

On the other way you can list files and check them one by one.

oneat
+1  A: 

The below function lists the file based on their creation date:

function listdir_by_date($path){
$dir = opendir($path);
$list = array();

while($file = readdir($dir)){
    if ($file != '.' and $file != '..'){
        // add the filename, to be sure not to
        // overwrite a array key
        $ctime = filectime($data_path . $file) . ',' . $file;
        $list[$ctime] = $file;
    }
}
closedir($dir);
krsort($list);
return $list;
}

Example:

$list = listdir_by_date($path);

Now you can loop through the list to see their dates and delete accordingly:

foreach ($list as $exp_date)
{

  $expiration_date = strtotime($exp_date);

  if ($expiration_date > 24*3600)
  {
    unlink ($path);
  }
}
Sarfraz
+1  A: 

I think you could go about this by looping through the directory with readdir and delete based on the timestamp:

<?php
if ($handle = opendir('/path/to/files')) {

    while (false !== ($file = readdir($handle))) { 
        $filelastmodified = filemtime($file);

        if(($filelastmodified-time()) > 24*3600)
        {
           unlink($file);
        }

    }

    closedir($handle); 
}
?>

The if(($filelastmodified-time()) > 24*3600) will select files older than 24 hours (24 hours times 3600 seconds per hour). If you wanted days, it should read for example 7*24*3600 for files older than a week.

Also, note that filemtime returns the time of last modification of the file, instead of creation date.

Pawel J. Wal
A: 

Try SplIterators

// setup timezone and get timestamp for yesterday
date_default_timezone_set('Europe/Berlin'); // change to yours
$yesterday = strtotime('-1 day', time());

// setup path to cache dir and initialize iterator
$path      = realpath('/path/to/files'); // change to yours
$objects   = new RecursiveIteratorIterator(
                 new RecursiveDirectoryIterator($path));

// iterate over files in directory and delete them
foreach($objects as $name => $object){
    if ($object->isFile() && ($object->getCTime() < $yesterday)) {
        // unlink($object);
        echo PHP_EOL, 'deleted ' . $object;
    }
}

Creation Time is only available on Windows.

Gordon