Last active
September 24, 2024 18:47
-
-
Save eusonlito/5099936 to your computer and use it in GitHub Desktop.
PHP function to get the folder size including subfolders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function folderSize ($dir) | |
{ | |
$size = 0; | |
foreach (glob(rtrim($dir, '/').'/*', GLOB_NOSORT) as $each) { | |
$size += is_file($each) ? filesize($each) : folderSize($each); | |
} | |
return $size; | |
} |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot