-
-
Save Haran/778a35bda48e1e5901a446b516caca33 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 | |
/** | |
* 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