Skip to content

Instantly share code, notes, and snippets.

@AnthoniG
Forked from eusonlito/foldersize.php
Last active March 16, 2022 14:38
Show Gist options
  • Save AnthoniG/969556c5354727cb5d658372e203bf60 to your computer and use it in GitHub Desktop.
Save AnthoniG/969556c5354727cb5d658372e203bf60 to your computer and use it in GitHub Desktop.
PHP function to get the folder size including subfolders
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]);
}
<?php
function folderSize ($dir)
{
$size = 0;
foreach (glob(rtrim($dir, '/').'/*', GLOB_NOSORT) as $each) {
$size += is_file($each) ? filesize($each) : folderSize($each);
}
return $size;
}
$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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment