加入收藏 | 设为首页 | 会员中心 | 我要投稿 莱芜站长网 (https://www.0634zz.com/)- 云连接、建站、智能边缘云、设备管理、大数据!
当前位置: 首页 > 编程开发 > PHP > 正文

从php.ini中的简写字节符号获取字节值

发布时间:2020-08-16 06:06:02 所属栏目:PHP 来源:互联网
导读:有没有办法从使用 shorthand byte notation的ini_get(‘upload_max_filesize’)和ini_get(‘post_max_size’)函数返回的字符串中获取字节值?例如从4M获得4194304?我可以把这个功能搞到一起,但是如果没有这样做的话,我会感到惊讶. The paragraph you linked

有没有办法从使用 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源使用与上述类似的功能.

(编辑:莱芜站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读