如何以字节为单位进行最大文件上传?

时间:2022-09-26 21:21:57

I'm using ini_get('upload_max_filesize') to get the max file upload size.
The result is 5M.

我使用ini_get('upload_max_filesize')来获取最大文件上传大小。结果是5米。

What is the easiest way to get this in bytes?

用字节表示的最简单的方法是什么?

2 个解决方案

#1


19  

You could use the return_bytes from the documentation:

您可以使用文档中的return_bytes:

function return_bytes($val) {
    $val = trim($val);
    $last = strtolower($val[strlen($val)-1]);
    switch($last) {
        // The 'G' modifier is available since PHP 5.1.0
        case 'g':
            $val *= (1024 * 1024 * 1024); //1073741824
            break;
        case 'm':
            $val *= (1024 * 1024); //1048576
            break;
        case 'k':
            $val *= 1024;
            break;
    }

    return $val;
}

return_bytes(ini_get('post_max_size'));

#2


1  

For PHP 7 the solution will return: 'A non well formed numeric value encountered'

对于PHP 7,解决方案将返回:“遇到的非格式良好的数值”

It might be used:

它可能使用:

function return_bytes($val)
{
    preg_match('/(?<value>\d+)(?<option>.?)/i', trim($string), $matches);
    $inc = array(
        'g' => 1073741824, // (1024 * 1024 * 1024)
        'm' => 1048576, // (1024 * 1024)
        'k' => 1024
    );

    $value = (int) $matches['value'];
    $key = strtolower(trim($matches['option']));
    if (isset($inc[$key])) {
        $value *= $inc[$key];
    }

    return $value;
}

return_bytes(ini_get('post_max_size'));

#1


19  

You could use the return_bytes from the documentation:

您可以使用文档中的return_bytes:

function return_bytes($val) {
    $val = trim($val);
    $last = strtolower($val[strlen($val)-1]);
    switch($last) {
        // The 'G' modifier is available since PHP 5.1.0
        case 'g':
            $val *= (1024 * 1024 * 1024); //1073741824
            break;
        case 'm':
            $val *= (1024 * 1024); //1048576
            break;
        case 'k':
            $val *= 1024;
            break;
    }

    return $val;
}

return_bytes(ini_get('post_max_size'));

#2


1  

For PHP 7 the solution will return: 'A non well formed numeric value encountered'

对于PHP 7,解决方案将返回:“遇到的非格式良好的数值”

It might be used:

它可能使用:

function return_bytes($val)
{
    preg_match('/(?<value>\d+)(?<option>.?)/i', trim($string), $matches);
    $inc = array(
        'g' => 1073741824, // (1024 * 1024 * 1024)
        'm' => 1048576, // (1024 * 1024)
        'k' => 1024
    );

    $value = (int) $matches['value'];
    $key = strtolower(trim($matches['option']));
    if (isset($inc[$key])) {
        $value *= $inc[$key];
    }

    return $value;
}

return_bytes(ini_get('post_max_size'));