Skip to content

How to Show Human-Readable File Sizes in PHP and Laravel

Turn byte counts into sizes like 1.5 MB in PHP and Laravel, choose between decimal KB and binary KiB, and avoid the rounding bug in the usual snippet.

PHPPublished
On this page

filesize() returns bytes, and nobody wants to read "1048576" next to a download link. Turning bytes into "1 MB" looks like a five-line function, but there are two decisions hidden in it: whether a kilobyte is 1,000 or 1,024 bytes, and what happens when rounding reaches the next unit. This guide covers both, in plain PHP and in Laravel.

Quick Answer / TL;DR: Divide by 1,000 for KB, MB, GB (what macOS and storage vendors show) or by 1,024 and label the result KiB, MiB, GiB. Round, then move to the next unit if the rounded value reached it. Laravel's Number::fileSize() divides by 1,024 but labels the result KB.

#KB or KiB?

Two systems are in use, and they differ by 2.4% at KB and 7.4% at GB:

UnitBytesUsed by
kilobyte (KB)1,000SI prefixes; macOS Finder, disk and network vendors
kibibyte (KiB)1,024IEC binary prefixes; Linux tools with -h, memory

The IEC defined KiB, MiB and GiB so that "KB" could keep meaning 1,000 bytes (NIST: prefixes for binary multiples). Windows Explorer still divides by 1,024 and writes "KB". Pick one system, and label it correctly: a "1.5 MB" upload limit that's really 1.5 MiB rejects files your users think are allowed.

#The Native Approach

PHP has no built-in byte formatter, so most projects carry a helper like this:

1function humanFileSize(int $bytes, int $decimals = 1): string
2{
3 $units = ['B', 'KB', 'MB', 'GB', 'TB'];
4 $power = $bytes > 0 ? min((int) floor(log($bytes, 1000)), count($units) - 1) : 0;
5 
6 return round($bytes / 1000 ** $power, $decimals) . ' ' . $units[$power];
7}
8 
9echo humanFileSize(1500); // 1.5 KB
10echo humanFileSize(1048576); // 1 MB
11echo humanFileSize(1500000000); // 1.5 GB

It's decimal, short, and right most of the time.

#Where It Breaks Down

The unit is chosen before rounding, so a value just under the next unit rounds up to a number that should have been the next unit:

1echo humanFileSize(999999); // 1000 KB

The fix is to round first and step up when the rounded value reaches the base. The other gaps are smaller: negative sizes (a difference between two files) make log() fail, the result isn't localized ("1,5 KB" in German), and there's no way back from "10 MB" to bytes for a config value.

#A Better Native Version

Round, then move up a unit when rounding reached it, and keep the sign:

1function humanFileSize(int $bytes, int $decimals = 1, bool $binary = false): string
2{
3 $base = $binary ? 1024 : 1000;
4 $units = $binary ? ['B', 'KiB', 'MiB', 'GiB', 'TiB'] : ['B', 'KB', 'MB', 'GB', 'TB'];
5 $absolute = abs($bytes);
6 $unit = 0;
7 
8 while ($unit < count($units) - 1 && $absolute >= $base ** ($unit + 1)) {
9 $unit++;
10 }
11 
12 $value = round($absolute / $base ** $unit, $decimals);
13 
14 if ($value >= $base && $unit < count($units) - 1) {
15 $value = round($absolute / $base ** ++$unit, $decimals);
16 }
17 
18 return ($bytes < 0 ? '-' : '') . $value . ' ' . $units[$unit];
19}
20 
21echo humanFileSize(999999); // 1 MB
22echo humanFileSize(1536, binary: true); // 1.5 KiB
23echo humanFileSize(-2500); // -2.5 KB

For localized decimals, format $value with NumberFormatter from ext-intl instead of concatenating it.

#In Laravel

Laravel 11 and later ship Number::fileSize():

1use Illuminate\Support\Number;
2 
3echo Number::fileSize(1048576); // 1 MB
4echo Number::fileSize(1536, 2); // 1.50 KB
5echo Number::fileSize(1500); // 1 KB

Know what it does before showing it to users. It divides by 1,024 but labels the result KB, MB, GB. Its default precision is 0, so 1,500 bytes shows as "1 KB" and 1,536 as "2 KB". It switches unit once the value passes 0.9 of the next one, so 999 bytes is "1 KB". For a dashboard that's fine; for an upload limit or a billing page, pass a precision and say which unit you mean.

#Using a Package

Pharaonic Readable formats both systems with the rounding rule above, localizes through ext-intl when you pass a locale, and parses sizes back into bytes:

1use Pharaonic\Readable\Bytes;
2 
3echo Bytes::format(1500); // 1.5 KB
4echo Bytes::format(999999); // 1 MB
5echo Bytes::format(1536, binary: true); // 1.5 KiB
6 
7Bytes::parse('10 MB'); // 10000000
8Bytes::parse('1.5 KiB'); // 1536

It's worth it when sizes appear in several places, when you need both directions (a "max upload: 10 MB" setting that code compares against bytes), or when you show sizes in more than one language. For one label on one page, the native function is enough.

#Which One to Use

You needUse
A size label in plain PHPThe native function above (round, then step up)
A quick size in a Laravel view where the exact unit doesn't matterNumber::fileSize() with a precision
Decimal and binary units, localized output, or parsing sizes backA formatter such as Pharaonic Readable

#Conclusion

Formatting bytes is easy once two choices are explicit: decimal KB or binary KiB, and rounding before choosing the unit. Write them into one helper, label the unit honestly, and test the boundary values (999, 1000, 999999) once. Sources: PHP filesize(), NIST binary prefixes, Laravel Number::fileSize().

PHP

How to Generate a Stable Hash for PHP Arrays

Learn how to generate deterministic hashes for PHP arrays, normalize nested data, avoid key-order issues, and create reliable structured-data fingerprints.

Was this page helpful?

Something wrong or missing? Report it on GitHub or suggest an edit .