Skip to content

Commit c596d64

Browse files
Improve toKilobytes to handle spaces and case-insensitive units (#55019)
* Improve `toKilobytes` to handle spaces and case-insensitive units in File rule validation * Update File.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent d7c898f commit c596d64

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Illuminate/Validation/Rules/File.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ protected function toKilobytes($size)
217217
return $size;
218218
}
219219

220+
$size = strtolower(trim($size));
221+
220222
$value = floatval($size);
221223

222224
return round(match (true) {

tests/Validation/ValidationFileRuleTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,27 @@ public function testItCanSetDefaultUsing()
418418
);
419419
}
420420

421+
public function testFileSizeConversionWithDifferentUnits()
422+
{
423+
$this->passes(
424+
File::image()->size('5MB'),
425+
UploadedFile::fake()->create('foo.png', 5000)
426+
);
427+
428+
$this->passes(
429+
File::image()->size(' 2gb '),
430+
UploadedFile::fake()->create('foo.png', 2 * 1000000)
431+
);
432+
433+
$this->passes(
434+
File::image()->size('1Tb'),
435+
UploadedFile::fake()->create('foo.png', 1000000000)
436+
);
437+
438+
$this->expectException(\InvalidArgumentException::class);
439+
File::image()->size('10xyz');
440+
}
441+
421442
protected function setUp(): void
422443
{
423444
$container = Container::getInstance();

0 commit comments

Comments
 (0)