note 58410 deleted from function.filesize by aidan
| From: | aidan@php.net | Date: | Wed, 09 Nov 2005 06:15:46 +0000 |
| Subject: | note 58410 deleted from function.filesize by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-98304@lists.php.net to get a copy of this message | ||
Note Submitter: Jerry Hathaway
----
<?php
//Recursively calculate used space
function du($location) {
if (!$location || !is_dir($location))
return 0;
$size = 0;
$files = 0;
$dirs = 0;
$all = opendir($location);
while ($file = readdir($all)) {
if (is_dir($location.'\\'.$file) and $file != ".." and $file != ".")
{
$temp = du($location.'/'.$file);
$dirs++;
$size += $temp['size'];
$files += $temp['files'];
$dirs += $temp['dirs'];
unset($temp);
unset($file);
} elseif (!is_dir($location.'\\'.$file)) {
$stats = stat($location.'\\'.$file);
$size += $stats['size'];
$files++;
unset($file);
}
}
closedir($all);
unset($all);
return array('size' => $size, 'files' => $files, 'dirs' =>
$dirs);
}
?>
I can't take credit for this completely, I found it around here somewhere. I modified it to
return an array that not only contains a folder's size, but the number of files and folders in
it as well. You'll have to change the '\\' to '/' for *nix systems, of
course.
Example usage:
<?php
$info = du('/path/to/somewhere);
echo $info['size']; //the total space occupied
echo $info['files']; //the total number of files in it
echo $info['dirs']; //the total number of folders in it
?>