Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the max file upload limit in php

Tags:

php

How can the file upload size allowed by php settings be determined withing a php script?

like image 647
HyperCas Avatar asked May 15 '10 16:05

HyperCas


People also ask

What is the maximum upload file size in PHP?

To increaes file upload size in PHP, you need to modify the upload_max_filesize and post_max_size variable's in your php. ini file. In addition, you can also set the maximum number of files allowed to be uploaded simultaneously, in a single request, using the max_file_uploads . Note that from PHP 5.3.

How can upload file size in PHP?

The filesize() function in PHP is an inbuilt function which is used to return the size of a specified file. The filesize() function accepts the filename as a parameter and returns the size of a file in bytes on success and False on failure.


5 Answers

The upload is limited by three options: upload_max_filesize, post_max_size and memory_limit. Your upload is only done if it doesn't exeed one of them.

The ini_get() function provides you with a short hand of the limit and should be converted first. Thx to AoEmaster for this.

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;
}

function max_file_upload_in_bytes() {
    //select maximum upload size
    $max_upload = return_bytes(ini_get('upload_max_filesize'));
    //select post limit
    $max_post = return_bytes(ini_get('post_max_size'));
    //select memory limit
    $memory_limit = return_bytes(ini_get('memory_limit'));
    // return the smallest of them, this defines the real limit
    return min($max_upload, $max_post, $memory_limit);
}

Source: http://www.kavoir.com/2010/02/php-get-the-file-uploading-limit-max-file-size-allowed-to-upload.html

like image 195
HyperCas Avatar answered Oct 04 '22 21:10

HyperCas


Use ini_get to get the current configuration value:

ini_get('upload_max_filesize')
like image 26
Gumbo Avatar answered Oct 04 '22 22:10

Gumbo


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;
}

function get_upload_max_filesize()
{
    $max_upload = return_bytes(ini_get('upload_max_filesize'));
    $max_post = return_bytes(ini_get('post_max_size'));
    return min($max_upload, $max_post, $memory_limit);
}
like image 24
AoEmaster Avatar answered Oct 04 '22 21:10

AoEmaster


You can also change that size at runtime using the .htaccess file without the need to change your php.ini file

php_value upload_max_filesize 1224M
php_value post_max_size 1224M
php_value max_execution_time 3000
php_value max_input_time 3000

copy that code and put your file, then store that file with index file then you run your project you also capable to upload 1GB file

for more detail read this article

like image 45
Renish Khunt Avatar answered Oct 04 '22 23:10

Renish Khunt


Here's a single function, implementing the original idea from AoEmaster. The function returns integer (amount of bytes).

function _GetMaxAllowedUploadSize(){
    $Sizes = array();
    $Sizes[] = ini_get('upload_max_filesize');
    $Sizes[] = ini_get('post_max_size');
    $Sizes[] = ini_get('memory_limit');
    for($x=0;$x<count($Sizes);$x++){
        $Last = strtolower($Sizes[$x][strlen($Sizes[$x])-1]);
        if($Last == 'k'){
            $Sizes[$x] *= 1024;
        } elseif($Last == 'm'){
            $Sizes[$x] *= 1024;
            $Sizes[$x] *= 1024;
        } elseif($Last == 'g'){
            $Sizes[$x] *= 1024;
            $Sizes[$x] *= 1024;
            $Sizes[$x] *= 1024;
        } elseif($Last == 't'){
            $Sizes[$x] *= 1024;
            $Sizes[$x] *= 1024;
            $Sizes[$x] *= 1024;
            $Sizes[$x] *= 1024;
        }
    }
    return min($Sizes);
}

If you want, you can combine it with the following function that renders the output as a human readable text.

function _Byte2Size($bytes,$RoundLength=1) {
    $kb = 1024;         // Kilobyte
    $mb = 1024 * $kb;   // Megabyte
    $gb = 1024 * $mb;   // Gigabyte
    $tb = 1024 * $gb;   // Terabyte

    if($bytes < $kb) {
        if(!$bytes){
            $bytes = '0';
        }
        return (($bytes + 1)-1).' B';
    } else if($bytes < $mb) {
        return round($bytes/$kb,$RoundLength).' KB';
    } else if($bytes < $gb) {
        return round($bytes/$mb,$RoundLength).' MB';
    } else if($bytes < $tb) {
        return round($bytes/$gb,$RoundLength).' GB';
    } else {
        return round($bytes/$tb,$RoundLength).' TB';
    }
}

Use it this way:

echo 'Max allowed upload size: '._Byte2Size(_GetMaxAllowedUploadSize());

A result could be:

Max allowed upload size: 500 MB

like image 42
Sebastian Unterberg Avatar answered Oct 04 '22 22:10

Sebastian Unterberg