note 72700 deleted from function.filesize by aidan
| From: | aidan@php.net | Date: | Sat, 02 May 2009 11:18:03 +0000 |
| Subject: | note 72700 deleted from function.filesize by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154122@lists.php.net to get a copy of this message | ||
Note Submitter: wielkiegie at gmail dot com
----
There is a lot of functions for size of dir. But drive operations are slow, so if you want use those
functions many times in one script, it's too slow. You can use this small function instead:
<?php
function dirsize($dir,$buf=2)
{
static $buffer;
if(isset($buffer[$dir]))
return $buffer[$dir];
if(is_file($dir))
return filesize($dir);
if($dh=opendir($dir))
{
$size=0;
while(($file=readdir($dh))!==false)
{
if($file=='.' || $file=='..')
continue;
$size+=dirsize($dir.'/'.$file,$buf-1);
}
closedir($dh);
if($buf>0)
$buffer[$dir]=$size;
return $size;
}
return false;
}
?>
What does it do? It save size in static array to cache result. You can choose deep value (e.q. if
you want cache only first dir, use dirsize($dirname,1);).