Skip to content

Commit

Permalink
IHF: format_bytes helper refactored.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-ivanov committed Jun 25, 2016
1 parent 93b48cd commit 668b0eb
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
if (!function_exists('format_bytes')) {
function format_bytes($bytes, $precision = 2)
{
$units = ['B', 'kB', 'MB', 'GB', 'TB'];
$units = ['B', 'KB', 'MB', 'GB', 'TB'];

$maxFactor = count($units) - 1;
$factor = floor(((strlen($bytes) - 1) / 3));
$factor = ($factor > $maxFactor) ? $maxFactor : $factor;
$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 / pow(1024, $factor)), $precision) . $units[$factor];
return round($bytes, $precision) . ' ' . $units[$pow];
}
}

0 comments on commit 668b0eb

Please sign in to comment.