public static formatBytes ( double $bytes, integer $precision = 2 ) : string | ||
$bytes | double | number of bytes |
$precision | integer | the number of decimal places to round off |
return | string |
public static function formatBytes($bytes, $precision = 2)
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}