微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

原文地址:https://www.jb51.cc/php/130462.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐