跳到正文
DEV Community· Fun Fat Studios·· 2 小時前AI 評分60

為何顯示「49 KB」的檔案實際是 50.5 KB?KB 與 KiB 的差異會造成上載限制落差

Your "49 KB" file is actually 50.5 KB. Blame the kilobyte.

AI 導讀

文章説明,50,500 bytes 的檔案在以 1,024 bytes 為單位的系統中可顯示為「49 KB」,但會超出 50,000 bytes 的上載上限。KB 可指 1,000 bytes,KiB 則指 1,024 bytes;PHP 和 nginx 的 `50K` 設定採 1,024 bytes 計算,與 50,000 bytes 的程式檢查不同。

正文

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

  1. Write limits in bytes in your config and code. 51200 or 50000. Pick one on purpose, not by accident of shorthand.
  2. 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.
  3. 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.

  1. 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

Disclosure: this article was drafted with AI assistance and fact-checked against the linked docs.

來源:DEV Community · dev.to