function size($path, $formated = true, $retstring = null){
if(!is_dir($path) || !is_readable($path)){
if(is_file($path) || file_exists($path)){
$size = filesize($path);
} else {
return false;
}
} else {
$path_stack[] = $path;
$size = 0;
do {
$path = array_shift($path_stack);
$handle = opendir($path);
while(false !== ($file = readdir($handle))) {
if($file != '.' && $file != '..' && is_readable($path . DIRECTORY_SEPARATOR . $file)) {
if(is_dir($path . DIRECTORY_SEPARATOR . $file)){ $path_stack[] = $path . DIRECTORY_SEPARATOR . $file; }
$size += filesize($path . DIRECTORY_SEPARATOR . $file);
}
}
closedir($handle);
} while (count($path_stack)> 0);
}
if($formated){
$sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
if($retstring == null) { $retstring = '%01.2f %s'; }
$lastsizestring = end($sizes);
foreach($sizes as $sizestring){
if($size <1024){ break; }
if($sizestring != $lastsizestring){ $size /= 1024; }
}
if($sizestring == $sizes[0]){ $retstring = '%01d %s'; } // los Bytes normalmente no son fraccionales
$size = sprintf($retstring, $size, $sizestring);
}
return $size;
}