Skip to content

Instantly share code, notes, and snippets.

@Haran
Forked from eusonlito/foldersize.php
Last active July 5, 2017 13:26
Show Gist options
  • Save Haran/778a35bda48e1e5901a446b516caca33 to your computer and use it in GitHub Desktop.
Save Haran/778a35bda48e1e5901a446b516caca33 to your computer and use it in GitHub Desktop.
PHP function to get the folder size including subfolders
<?php
/**
* Retrieve folder size including subfolders
* and including 'hidden' files and folders
*
* @param string $dir Directory path
* @return int Size of the directory in bytes
*/
function folderSize($dir)
{
$size = 0;
$dir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR.'{,.}*';
$list = glob($dir, GLOB_BRACE);
$list = array_filter($list, function($v){
return preg_match('%(\\\\|/)\.{1,2}$%im', $v) ? false : true;
});
foreach ($list as $each) {
$size += is_file($each) ? filesize($each) : folderSize($each);
}
return $size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment