'How can I get max file upload in bytes?

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

What is the easiest way to get this in bytes?

php


Solution 1:[1]

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

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'));

Solution 2:[2]

Not enough rep to comment on the solution by @fierycat

Just need to change trim($string) to trim($val).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 FieryCat
Solution 2 Andrew Sharpe