有没有办法从使用
shorthand byte notation的ini_get(‘upload_max_filesize’)和ini_get(‘post_max_size’)函数返回的字符串中获取字节值?例如从4M获得4194304?我可以把这个功能搞到一起,但是如果没有这样做的话,我会感到惊讶.
The paragraph you linked to结束:
You may not use these shorthand notations outside of PHP.ini,instead
use an integer value of bytes. See 07001 for an
example on how to convert these values.
这会导致你这样的东西(我稍稍修改):
function return_bytes($val)
{
$val = trim($val);
$last = strtolower($val[strlen($val)-1]);
$val = substr($val,-1); // necessary since PHP 7.1; otherwise optional
switch($last) {
// The 'G' modifier is available since PHP 5.1.0
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
使用它like so:
echo return_bytes("3M");
// Output: 3145728
没有内置函数执行此任务;回想一下,真正的INI设置是在PHP内部设计的. PHP源使用与上述类似的功能.