note 58410 added to function.filesize
| From: | JerryHathaway at osu1 dot php dot net | Date: | Thu, 03 Nov 2005 12:12:41 +0000 |
| Subject: | note 58410 added to function.filesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-98021@lists.php.net to get a copy of this message | ||
<?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
?>
----
Manual Page -- http://www.php.net/manual/en/function.filesize.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58410
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58410&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58410&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58410&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58410&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58410&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58410&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58410&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58410&report=yes
Search -- http://master.php.net/manage/user-notes.php