diff --git a/system/Files/File.php b/system/Files/File.php index 15b53262e9c2..67a1c3ce37f1 100644 --- a/system/Files/File.php +++ b/system/Files/File.php @@ -72,14 +72,18 @@ public function getSize() /** * Retrieve the file size by unit. + * Supports the metric units "kb" and "mb" + * and the IEC units "kib" and "mib". * * @return false|int|string */ public function getSizeByUnit(string $unit = 'b') { return match (strtolower($unit)) { - 'kb' => number_format($this->getSize() / 1024, 3), - 'mb' => number_format(($this->getSize() / 1024) / 1024, 3), + 'kb' => number_format($this->getSize() / 1000, 3), + 'kib' => number_format($this->getSize() / 1024, 3), + 'mb' => number_format(($this->getSize() / 1000) / 1000, 3), + 'mib' => number_format(($this->getSize() / 1024) / 1024, 3), default => $this->getSize(), }; } diff --git a/tests/system/Files/FileTest.php b/tests/system/Files/FileTest.php index 800a1ac278f1..862ffada7187 100644 --- a/tests/system/Files/FileTest.php +++ b/tests/system/Files/FileTest.php @@ -92,17 +92,31 @@ public function testCanAccessSplFileInfoMethods(): void $this->assertSame('file', $file->getType()); } - public function testGetSizeReturnsKB(): void + public function testGetSizeReturnsKiB(): void { $file = new File(SYSTEMPATH . 'Common.php'); $size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1024, 3); + $this->assertSame($size, $file->getSizeByUnit('kib')); + } + + public function testGetSizeReturnsKB(): void + { + $file = new File(SYSTEMPATH . 'Common.php'); + $size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1000, 3); $this->assertSame($size, $file->getSizeByUnit('kb')); } - public function testGetSizeReturnsMB(): void + public function testGetSizeReturnsMiB(): void { $file = new File(SYSTEMPATH . 'Common.php'); $size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1024 / 1024, 3); + $this->assertSame($size, $file->getSizeByUnit('mib')); + } + + public function testGetSizeReturnsMB(): void + { + $file = new File(SYSTEMPATH . 'Common.php'); + $size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1000 / 1000, 3); $this->assertSame($size, $file->getSizeByUnit('mb')); }