-
-
Save eusonlito/5099936 to your computer and use it in GitHub Desktop.
| <?php | |
| function folderSize ($dir) | |
| { | |
| $size = 0; | |
| foreach (glob(rtrim($dir, '/').'/*', GLOB_NOSORT) as $each) { | |
| $size += is_file($each) ? filesize($each) : folderSize($each); | |
| } | |
| return $size; | |
| } |
awasome function ๐ ๐ฏ
The result is in bytes or KB? Thank you!
@mdimitris " bytes"
Hope you must have got the answer ๐
Thanks
Thanks
many thanks.
<3
hi what is folderSize($each); I think this one should be undefined
@sakonachhoeurng Hi, you probably noticed already) but just in case, It's the function name - recursive call, to count all subfolders as fell.
Perfect.
Perfect - thanks for your help!
Nice, however, at least on Linux, it won't include dot-files (since * does not match .some-file). Furthermore, on my machine an empty directory still occupies some space (4096 bytes according to du), this is also not included in folderSize.
Oh, and thanks, I'm using something very similar.
Cool thank you
Thanks! It helps me a lot.
For directory size:
if($size<1024){$size=$size." Bytes";}
elseif(($size<1048576)&&($size>1023)){$size=round($size/1024, 1)." KB";}
elseif(($size<1073741824)&&($size>1048575)){$size=round($size/1048576, 1)." MB";}
else{$size=round($size/1073741824, 1)." GB";}
return $size;
Thanks a lot
Great, thanks! Using this!
Nice but nothing beats the Swag of the following when the directory size is large.
$f = './path/directory';
$io = popen ( '/usr/bin/du -sk ' . $f, 'r' );
$size = fgets ( $io, 4096);
$size = substr ( $size, 0, strpos ( $size, "\t" ) );
pclose ( $io );
echo 'Directory: ' . $f . ' => Size: ' . $size;
Gracias!!
@eusonlito Perfect ๐
Regards,
some fast solution for Windows?
This function converts bytes:
function sizeFilter($bytes)
{
$label = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
for ($i = 0; $bytes >= 1024 && $i < (count($label) - 1); $bytes /= 1024, $i++) ;
return (round($bytes, 2) . " " . $label[$i]);
}
Perfect..
Great!
Va perfecto, gracias ๐
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
$f = './path/directory';
$io = popen ( '/usr/bin/du -sh ' . $f, 'r' );
$size = fgets ( $io, 4096);
$size = substr ( $size, 0, strpos ( $size, "\t" ) );
pclose ( $io );
echo 'Directory: ' . $f . ' => Size: ' . $size;
In GigaBytes:
return $size / 1e-9; //GigaBytes
well Done! And thank you :)