Skip to content

Commit 668b0eb

Browse files
committed
IHF: format_bytes helper refactored.
1 parent 93b48cd commit 668b0eb

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/format.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
if (!function_exists('format_bytes')) {
44
function format_bytes($bytes, $precision = 2)
55
{
6-
$units = ['B', 'kB', 'MB', 'GB', 'TB'];
6+
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
77

8-
$maxFactor = count($units) - 1;
9-
$factor = floor(((strlen($bytes) - 1) / 3));
10-
$factor = ($factor > $maxFactor) ? $maxFactor : $factor;
8+
$bytes = max($bytes, 0);
9+
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
10+
$pow = min($pow, count($units) - 1);
11+
$bytes /= pow(1024, $pow);
1112

12-
return round(($bytes / pow(1024, $factor)), $precision) . $units[$factor];
13+
return round($bytes, $precision) . ' ' . $units[$pow];
1314
}
1415
}

0 commit comments

Comments
 (0)