Laravel uses 'Kilobyte' and 'Kibibyte' interchangeably #56799
Replies: 2 comments
-
I fully back any work you can do to make this clearer and/or more correct. https://github.com/laravel/framework/blob/12.x/src/Illuminate/Support/Number.php#L206 |
Beta Was this translation helpful? Give feedback.
-
That’s a really solid observation. I’ve also noticed the inconsistency between how Laravel treats kilobytes in validation rules vs. fake file uploads. In practice, most developers think of “KB” as 1024 bytes, but since the docs and method names explicitly say kilobytes, it ends up feeling inconsistent and a bit confusing. I agree that the best way forward would be to make it clear in the documentation when 1000 vs 1024 is being used, and maybe even allow KiB notation for those who need precision. That would keep existing behavior intact while giving developers more clarity. Thanks for pointing this out even small details like this can really improve developer experience. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I wanted to bring up a small frustration I have with the framework. In documentation, code comments, and parameter names, the words "kilobyte" (KB 1000) and "kibibyte" (KiB 1024) are used interchangeably.
First, let's look at a place that uses it correctly:
File::types(['txt'])->max('5kb')
The file validation rule uses the kb prefix. This is technically kilobit, but glossing over that - KB should mean it uses a base of 1000. Looking at the code, this is correct:
toKilobytes:
The name of this method is
toKilobytes
, which is also correct.Now for somewhere that uses it incorrectly:
UploadedFile::fake()->create('a')->size(5);
If we look at this
->size
function, the name of the parameter iskilobytes
. But looking at the function definition, we see this:A base of 1024. So here, a "kilobyte" is being used as slang for what is really a kibibyte. What about a more egregious example?
file|size:50
Validation Rule or themax:
Validation RuleThe laravel documentation reads:
But looking at the definition of the
size:
rule, we see:They're kibibytes again! Not only does this violate what the docs tell us, it's also inconsistent with how
File::types()->max()
behaves when they're supposedly the same measurement.Proposed solution
I know realistically we all use these names wrong. They're also very close in size until you start getting into the GiB / GB range. But it would be nice to have a disclaimer in the docs that point out when a method is using one versus another, and maybe even grow
File::types()->max()
to accept the"5KiB"
versions as well.Beta Was this translation helpful? Give feedback.
All reactions