get upload max file size in php

To get the maximum file size allowed for uploading in PHP, you can use the ini_get function with the upload_max_filesize option. This will return the value of the configuration directive in bytes. You can then convert the bytes to the desired format (e.g. KB, MB, GB).

Here's an example code snippet that gets the upload_max_filesize value in bytes:

main.php
$maxFileSize = ini_get('upload_max_filesize');
$maxFileSizeBytes = return_bytes($maxFileSize); // convert to bytes

function return_bytes($val) {
    $val = trim($val);
    $last = strtolower($val[strlen($val)-1]);
    switch($last) {
        case 'g':
            $val *= 1024;
        case 'm':
            $val *= 1024;
        case 'k':
            $val *= 1024;
    }
    return $val;
}
392 chars
17 lines

In the above code, the return_bytes function is used to convert the file size value to bytes. It checks for the last character of the string (e.g. "M" for megabytes) and multiplies the size accordingly.

gistlibby LogSnag