Re: dir size
| From: | Nathan Taylor | Date: | Fri, 03 Oct 2003 19:38:31 +0000 |
| Subject: | Re: dir size | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-164962@lists.php.net to get a copy of this message | ||
Hey, I think I mis-understood you and my first version didn't do what you want, so I added what
you needed.
<?php
function DirStat($directory) {
global $FolderCount, $FileCount, $FolderSize;
chdir($directory);
$directory = getcwd();
$open = opendir($directory);
if($open) {
while($file = readdir($open)) {
if($file == ".." || $file == ".") continue;
if(is_file($file)) {
$FileCount++;
$FolderSize += filesize($file);
} elseif(is_dir($file)) {
$FolderCount++;
}
}
if($FolderCount > 0) {
$open2 = opendir($directory);
while($folders = readdir($open2)) {
$folder = $directory."/".$folders;
if($folders == ".." || $folders == ".") continue;
if(is_dir($folder)) {
echo DirStat($folder);
}
}
closedir($open2);
}
closedir($open);
}
}
function ByteSize($bytes) {
$size = $bytes / 1024;
if($size < 1024){
$size = number_format($size, 2);
$size .= "kb";
} else {
if($size / 1024 < 1024) {
$size = number_format($size / 1024, 2);
$size .= "mb";
} else {
$size = number_format($size / 1024 / 1024, 2);
$size .= "gb";
}
}
return $size;
}
$dir = getcwd();
DirStat("tmp", 0);
chdir($dir);
$FolderSize = ByteParse($FolderSize);
echo $FileCount;
echo "</p>\n";
echo $FolderSize;
?>
The DirStat() function is almost unchanged except that it now passes out the byte size of the
directories read. I also threw in a chdir() to get you back to your working directory. The major
addition here is the ByteSize() function. That function takes the integer which is your total size
of the directory and converts it into a nice formatted number either in kb, mb, or gb accordingly.
You'll find it uses true file size as well, not "industry standard" sizing.
Enjoy,
Nathan Taylor
----- Original Message -----
From: Ms Carlsson
To: php-general@lists.php.net
Sent: Friday, October 03, 2003 2:44 PM
Subject: [PHP] dir size
is it possible to count a size of a dir and all sub dirs in php ? and if,
how?
thanx
_________________________________________________________________
Lättare att hitta drömresan med MSN Resor http://www.msn.se/resor/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php