Fix #19526: Add the convertIniSizeToBytes method to BaseStringHelper

This commit is contained in:
Maksim Spirkov
2025-09-30 02:27:40 +04:00
committed by GitHub
parent 54fb1cc7af
commit d8ba4c0468
5 changed files with 55 additions and 44 deletions

View File

@ -37,6 +37,7 @@ Yii Framework 2 Change Log
- Bug #19655: Fix `LinkPager::getPageRange` when `maxButtons` is 2 (max-s-lab)
- Enh #20539: Update minimum PHP version requirement from `7.3` to `7.4` (terabytesoftw)
- Bug #20541: Remove deprecated caching components: `XCache` and `ZendDataCache`, and update related tests and documentation (terabytesoftw)
- Enh #19526: Add the `convertIniSizeToBytes` method to `BaseStringHelper` (max-s-lab)
2.0.53 June 27, 2025

View File

@ -52,6 +52,30 @@ class BaseStringHelper
return mb_substr((string)$string, $start, $length, '8bit');
}
/**
* Converts php.ini style size to bytes.
*
* @param string $string php.ini style size. Examples: `512M`, `1024K`, `1G`, `256`.
* @return int the number of bytes equivalent to the specified string.
* @since 2.0.54
*/
public static function convertIniSizeToBytes($string)
{
switch (substr($string, -1)) {
case 'M':
case 'm':
return (int) $string * 1048576;
case 'K':
case 'k':
return (int) $string * 1024;
case 'G':
case 'g':
return (int) $string * 1073741824;
default:
return (int) $string;
}
}
/**
* Returns the trailing name component of a path.
* This method is similar to the php function `basename()` except that it will

View File

@ -325,8 +325,8 @@ class FileValidator extends Validator
public function getSizeLimit()
{
// Get the lowest between post_max_size and upload_max_filesize, log a warning if the first is < than the latter
$limit = $this->sizeToBytes(ini_get('upload_max_filesize'));
$postLimit = $this->sizeToBytes(ini_get('post_max_size'));
$limit = StringHelper::convertIniSizeToBytes(ini_get('upload_max_filesize'));
$postLimit = StringHelper::convertIniSizeToBytes(ini_get('post_max_size'));
if ($postLimit > 0 && $postLimit < $limit) {
Yii::warning('PHP.ini\'s \'post_max_size\' is less than \'upload_max_filesize\'.', __METHOD__);
$limit = $postLimit;
@ -351,29 +351,6 @@ class FileValidator extends Validator
return !($value instanceof UploadedFile) || $value->error == UPLOAD_ERR_NO_FILE;
}
/**
* Converts php.ini style size to bytes.
*
* @param string $sizeStr $sizeStr
* @return int
*/
private function sizeToBytes($sizeStr)
{
switch (substr($sizeStr, -1)) {
case 'M':
case 'm':
return (int) $sizeStr * 1048576;
case 'K':
case 'k':
return (int) $sizeStr * 1024;
case 'G':
case 'g':
return (int) $sizeStr * 1073741824;
default:
return (int) $sizeStr;
}
}
/**
* Checks if given uploaded file have correct type (extension) according current validator settings.
* @param UploadedFile $file

View File

@ -536,4 +536,25 @@ class StringHelperTest extends TestCase
['من صالح هاشمی هستم', 'من ', ' هستم', 'صالح هاشمی'], // other languages
];
}
/**
* @dataProvider provideConvertIniSizeToBytesData
*/
public function testConvertIniSizeToBytes(string $string, int $expectedResult): void
{
$this->assertEquals($expectedResult, StringHelper::convertIniSizeToBytes($string));
}
public static function provideConvertIniSizeToBytesData(): array
{
return [
['1024', 1024],
['512K', 524288],
['512k', 524288],
['128M', 134217728],
['128m', 134217728],
['2G', 2147483648],
['2g', 2147483648],
];
}
}

View File

@ -9,6 +9,7 @@ namespace yiiunit\framework\validators;
use Yii;
use yii\helpers\FileHelper;
use yii\helpers\StringHelper;
use yii\validators\FileValidator;
use yii\web\UploadedFile;
use yiiunit\data\validators\models\FakedValidationModel;
@ -78,7 +79,11 @@ class FileValidatorTest extends TestCase
public function testGetSizeLimit()
{
$size = min($this->sizeToBytes(ini_get('upload_max_filesize')), $this->sizeToBytes(ini_get('post_max_size')));
$size = min(
StringHelper::convertIniSizeToBytes(ini_get('upload_max_filesize')),
StringHelper::convertIniSizeToBytes(ini_get('post_max_size'))
);
$val = new FileValidator();
$this->assertEquals($size, $val->getSizeLimit());
$val->maxSize = $size + 1; // set and test if value is overridden
@ -91,23 +96,6 @@ class FileValidatorTest extends TestCase
$this->assertSame($_POST['MAX_FILE_SIZE'], $val->getSizeLimit());
}
protected function sizeToBytes($sizeStr)
{
switch (substr($sizeStr, -1)) {
case 'M':
case 'm':
return (int) $sizeStr * 1048576;
case 'K':
case 'k':
return (int) $sizeStr * 1024;
case 'G':
case 'g':
return (int) $sizeStr * 1073741824;
default:
return (int) $sizeStr;
}
}
public function testValidateAttributeMultiple()
{
$val = new FileValidator([
@ -370,7 +358,7 @@ class FileValidatorTest extends TestCase
} else {
$size = isset($param['size']) ? $param['size'] : random_int(
1,
$this->sizeToBytes(ini_get('upload_max_filesize'))
StringHelper::convertIniSizeToBytes(ini_get('upload_max_filesize'))
);
}
$type = isset($param['type']) ? $param['type'] : 'text/plain';