Your "49 KB" file is actually 50.5 KB. Blame the kilobyte.
A user picks a file. Their computer says 49 KB. Your form says "max 50 KB." The upload gets rejected anyway, and now there's a support ticket that says your site is broken.
It isn't broken, exactly. You and the user just disagree about what a kilobyte is. And honestly? Both of you are right.
Two kilobytes, one label
There are two definitions in everyday use:
- KB (kilobyte), SI/decimal: 1,000 bytes
- KiB (kibibyte), IEC/binary: 1,024 bytes
The IEC prefixes (KiB, MiB, GiB) exist precisely to separate the two. Almost nobody uses them in a UI, though. So "KB" on a screen could mean either one, and you usually can't tell which by looking.
Who counts which way
Windows Explorer uses 1,024 and calls it KB. Raymond Chen explained why on Microsoft's own blog years ago: everybody else said KB for 1,024 too, so Explorer followed existing practice. That's still the behaviour.
macOS went decimal. Apple's support page on storage capacity says OS X Leopard and earlier reported sizes in binary, which means everything from Snow Leopard (10.6) on counts 1 KB as 1,000 bytes. Same file, different number than on Windows.
Linux / GNU coreutils gives you both. ls -lh, du -h and df -h use powers of 1,024. Add --si and you get powers of 1,000. Here's a 51,000-byte file with coreutils 9.7:
$ ls -l photo.jpg -rw-r--r-- 1 box box 51000 Nov 19 12:48 photo.jpg $ ls -lh photo.jpg -rw-r--r-- 1 box box 50K Nov 19 12:48 photo.jpg $ ls -lh --si photo.jpg -rw-r--r-- 1 box box 51k Nov 19 12:48 photo.jpg Notice the capital K versus lowercase k. That's the only hint you get. Also notice -h printed 50K for something that's 49.8 KiB, so it rounded up. Rounding is its own little trap on top of everything else.
The arithmetic
Take a file that's exactly 50,500 bytes.
- Decimal: 50,500 / 1,000 = 50.5 KB
- Binary: 50,500 / 1,024 = 49.3 KiB
On a machine doing 1,024 math, that looks like a "49 KB" file. If your server rejects anything over 50,000 bytes, it fails. The user isn't lying and your code isn't buggy. You're just using different rulers.
The 51,000-byte file from above works the same way: 51.0 KB in decimal, 49.8 KiB in binary.
What the server actually counts
HTML can't enforce this for you. <input type="file"> supports accept, capture and multiple, and that's it. There's no size attribute. Any size limit lives in JavaScript or on the server, and the server is the one that counts.
And servers mostly think in 1,024.
PHP: upload_max_filesize and post_max_size accept shorthand. The PHP manual says plainly that 1K = 1,024 bytes and 1M = 1,048,576 bytes, and even adds a note that the IEC would call this a kibibyte. So upload_max_filesize = 50K means 51,200 bytes.
nginx: client_max_body_size 50k also means 51,200 bytes. The docs list k/K as kilobytes without saying how many bytes that is, but the parser in nginx's source (ngx_parse_size) multiplies by 1,024. Go over and you get a 413.
So here's the fun bit. A PHP or nginx config that says "50K" allows 51,200 bytes, while a hand-written check like if (size > 50000) allows 50,000. Two different limits, both called "50 KB." I'd bet plenty of apps have exactly this mismatch between their help text and their config.
Multipart overhead
One more wrinkle. nginx's client_max_body_size applies to the whole request body, not the file. PHP's post_max_size covers all the POST data too. A multipart/form-data body wraps each file in boundary lines plus Content-Disposition and Content-Type headers (RFC 7578 describes the format).
How much does that add? Sending that 51,000-byte file with curl -F produced a 51,199-byte body. About 200 bytes of overhead, which grows with longer field names, filenames and extra fields. Against a 51,200-byte nginx limit, that upload squeaks through by one byte. A file a couple hundred bytes bigger, still "under 50K" by any reading, gets a 413.
What to actually do
- Write limits in bytes in your config and code.
51200or50000. Pick one on purpose, not by accident of shorthand. - Show both in the UI if you show units at all. "Max 50,000 bytes (about 48.8 KiB)" is ugly but nobody will argue with it.
- Validate client-side with
File.size. MDN: it returns the size in bytes. No units to misread.
const MAX_BYTES = 50_000; input.addEventListener("change", () => { const file = input.files[0]; if (file && file.size > MAX_BYTES) { alert(`That file is ${file.size.toLocaleString()} bytes. The limit is ${MAX_BYTES.toLocaleString()}.`); input.value = ""; } }); That check is for the user's benefit only. Keep the server check, since anyone can skip your JavaScript.
- Leave a margin. If your server cap is 51,200 bytes for the whole body, don't advertise 51,200 bytes per file. Advertise less and leave room for multipart overhead and other form fields.
The main thing is to stop writing "KB" alone anywhere a limit gets enforced. Bytes are boring. Boring doesn't generate tickets.
Sources
- Microsoft, The Old New Thing: Why does Explorer use the term KB instead of KiB?
- Apple Support: How storage capacity is measured on Apple devices
- GNU coreutils manual: Block size
- PHP manual: Shorthand byte values (FAQ) and core php.ini directives
- nginx docs: client_max_body_size, measurement units, and ngx_parse.c source
- MDN: Blob.size and
<input type="file"> - RFC 7578: Returning Values from Forms: multipart/form-data
Disclosure: this article was drafted with AI assistance and fact-checked against the linked docs.
原文:https://www.a8q9.cn/en/article/your-49-kb-file-is-actually-50-5-kb-blame-the-kilobyte-dc2df9.html