有什么方法可以从像 ini_get('upload_max_filesize')
和 ini_get('post_max_size')
这样的函数返回的字符串中获取字节值吗? shorthand byte notation ?例如从 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 the ini_get() documentation for an example on how to convert these values.
这会引导您进行类似的操作(我稍作修改):
function return_bytes($val)
{
$val = trim($val);
if (is_numeric($val))
return $val;
$last = strtolower($val[strlen($val)-1]);
$val = substr($val, 0, -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 源代码使用与上述类似的函数。
关于byte - 从 php.ini 中的速记字节表示法获取字节值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6846445/