diff --git a/lib/Cake/Test/Case/Utility/CakeNumberTest.php b/lib/Cake/Test/Case/Utility/CakeNumberTest.php index 9a28eb8bf..8ebaad5de 100644 --- a/lib/Cake/Test/Case/Utility/CakeNumberTest.php +++ b/lib/Cake/Test/Case/Utility/CakeNumberTest.php @@ -642,6 +642,13 @@ class CakeNumberTest extends CakeTestCase { array(array('size' => '1.5MB', 'default' => false), 1572864), array(array('size' => '1GB', 'default' => false), 1073741824), array(array('size' => '1.5GB', 'default' => false), 1610612736), + array(array('size' => '1K', 'default' => false), 1024), + array(array('size' => '1.5K', 'default' => false), 1536), + array(array('size' => '1M', 'default' => false), 1048576), + array(array('size' => '1m', 'default' => false), 1048576), + array(array('size' => '1.5M', 'default' => false), 1572864), + array(array('size' => '1G', 'default' => false), 1073741824), + array(array('size' => '1.5G', 'default' => false), 1610612736), array(array('size' => '512', 'default' => 'Unknown type'), 512), array(array('size' => '2VB', 'default' => 'Unknown type'), 'Unknown type') ); diff --git a/lib/Cake/Utility/CakeNumber.php b/lib/Cake/Utility/CakeNumber.php index 88a96ba33..b02fc562a 100644 --- a/lib/Cake/Utility/CakeNumber.php +++ b/lib/Cake/Utility/CakeNumber.php @@ -111,26 +111,31 @@ class CakeNumber { /** * Converts filesize from human readable string to bytes * - * @param string $size Size in human readable string like '5MB' + * @param string $size Size in human readable string like '5MB', '5M', '500B', '50kb' etc. * @param mixed $default Value to be returned when invalid size was used, for example 'Unknown type' - * @return integer Bytes + * @return mixed Number of bytes as integer on success, `$default` on failure if not false * @throws CakeException On invalid Unit type. */ public static function fromReadableSize($size, $default = false) { if (ctype_digit($size)) { - return $size * 1; + return (int)$size; } $size = strtoupper($size); + $l = -2; $i = array_search(substr($size, -2), array('KB', 'MB', 'GB', 'TB', 'PB')); + if ($i === false) { + $l = -1; + $i = array_search(substr($size, -1), array('K', 'M', 'G', 'T', 'P')); + } if ($i !== false) { - $size = substr($size, 0, strlen($size) - 2); + $size = substr($size, 0, $l); return $size * pow(1024, $i + 1); } - if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, strlen($size) - 1))) { - $size = substr($size, 0, strlen($size) - 1); - return $size * 1; + if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, -1))) { + $size = substr($size, 0, -1); + return (int)$size; } if ($default !== false) {