function formatSize($bytes)
{
if ($bytes < 1024)
{
return $bytes.' b';
}
elseif ($bytes < 1048576)
{
return round($bytes / 1024, 2).' kb';
}
elseif ($bytes < 1073741824)
{
return round($bytes / 1048576, 2).' mb';
}
elseif ($bytes < 1099511627776)
{
return round($bytes / 1073741824, 2).' gb';
}
elseif ($bytes < 1125899906842624)
{
return round($bytes / 1099511627776, 2).' tb';
}
elseif ($bytes < 1152921504606846976)
{
return round($bytes / 1125899906842624, 2).' pb';
}
else
{
return 'impossible huge!';
}
}
function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
// Uncomment one of the following alternatives
// $bytes /= pow(1024, $pow);
// $bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . ' ' . $units[$pow];
}