public function getDirSize($dir, $format = true)
{
$dir_iterator = new RecursiveDirectoryIterator($dir);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
$size = 0;
foreach ($iterator as $file) {
$size += $file->getSize();
}
if ($format) {
if ($size / 1048576 > 1) {
return round($size / 1048576, 1) . ' MB';
// if the total size is bigger than 1 KB
} elseif ($size / 1024 > 1) {
return round($size / 1024, 1) . ' KB';
// else return the filesize in bytes
} else {
return round($size, 1) . ' bytes';
}
} else {
// return the total filesize in bytes
return $size;
}
}
usage:
$size = getDirSize('/dir/to/get/size/');
No comments:
Post a Comment
You can ask IT questions in comments!