PHP: Get the File Uploading Limit – Max File Size Allowed to Upload

by Yang Yang on February 23, 2010

PHP file upload max size is determined by 3 configuration values in php.ini, namely upload_max_filesize, post_max_size and memory_limit. You can get the maximum file size allowed in uploading by this snippet:

$max_upload = (int)(ini_get('upload_max_filesize'));
$max_post = (int)(ini_get('post_max_size'));
$memory_limit = (int)(ini_get('memory_limit'));
$upload_mb = min($max_upload, $max_post, $memory_limit);

Wherein $upload_mb is the maximum file size allowed for upload in MB. It’s the smallest of the 3 values. Just display this value beside the file upload control so the user knows the limit before choosing the file.

starak January 31, 2011 at 7:19 am

Great! Thanx 🙂

James Waples February 20, 2011 at 7:28 pm

Thank you very much for this! Such a time saver.

Wiktor Walc July 19, 2011 at 3:43 am

From PHP manual:

Note: When querying memory size values

Many ini memory size values, such as upload_max_filesize, are stored in the php.ini file in shorthand notation. ini_get() will return *the exact string stored in the php.ini file* and NOT its integer equivalent. Attempting normal arithmetic functions on these values will not have otherwise expected results.

Since not everyone will use the same notation for all three values, it’s better to convert all of them to bytes first, before finding min value.

Fery Ardiant January 9, 2012 at 3:04 am

Nice Thanks!

rdr March 1, 2012 at 12:54 am

-1, for missing that the ini variable can contain non-integral parts, as Wiktor noticed.

Jonas Äppelgran April 12, 2012 at 4:47 pm

upload_max_filesize is for _one_ file. When uploading multiple files at same time you can upload several files who together are bigger than the upload_max_filesize.

I believe this would be more interesting:

$max_upload = (int)(ini_get(‘upload_max_filesize’));
$max_post = (int)(ini_get(‘post_max_size’));
$max_memory = (int)(ini_get(‘memory_limit’));
$file_limit = min($max_upload, $max_post, $max_memory);
$total_limit = min($max_post, $max_memory);

Nelson Pérez March 18, 2013 at 3:09 am

Brother, thanks the peace of Christ for you, thanks for info 🙂

fakitol June 8, 2012 at 3:24 pm

Thank you for sharing this !

Hevyweb October 25, 2012 at 5:16 pm

You forgot, that in case if
upload_max_filesize – 900K
and post_max_size – 2,3M
You’ll get 900M
Or in case when upload_max_filesize – 5,1M and post_max_size – 5,7M you’ll get 5.

Yang Yang October 26, 2012 at 4:40 pm

What?

Paul Melekhov January 25, 2013 at 6:01 pm

You’re absolutely right! The topic starter didn’t rely on this cases. I wrote my own function that returns calculated max size of uploading file. It takes into account all your notes. Here it’s code:


/**
* Detects max size of file cab be uploaded to server
*
* Based on php.ini parameters "upload_max_filesize", "post_max_size" &
* "memory_limit". Valid for single file upload form. May be used
* as MAX_FILE_SIZE hidden input or to inform user about max allowed file size.
*
* @return int Max file size in bytes
*/
function detectMaxUploadFileSize()
{
/**
* Converts shorthands like "2M" or "512K" to bytes
*
* @param $size
* @return mixed
*/
$normalize = function($size) {
if (preg_match('/^([\d\.]+)([KMG])$/i', $size, $match)) {
$pos = array_search($match[2], array("K", "M", "G"));
if ($pos !== false) {
$size = $match[1] * pow(1024, $pos + 1);
}
}
return $size;
};
$max_upload = $normalize(ini_get('upload_max_filesize'));
$max_post = $normalize(ini_get('post_max_size'));
$memory_limit = $normalize(ini_get('memory_limit'));
$maxFileSize = min($max_upload, $max_post, $memory_limit);
return $maxFileSize;
}

— Paul Melekhov aKa gugglegum, Russia.

Paul Melekhov April 19, 2013 at 7:58 pm

I have an important note to my previous message with function detectMaxUploadFileSize(). This function does not respect some special php.ini values that treats as unlimited values. For example:
memory_limit = -1
post_max_size = 0

My function need to be rewrited, but I have no time at this moment. You can contact me via email gugglegum at gmail.com

lostinscope May 15, 2013 at 3:39 pm

as i am going to use the snippet Paul kindly shared i hereby share modified version
/**
* Detects max size of file cab be uploaded to server
*
* Based on php.ini parameters “upload_max_filesize”, “post_max_size” &
* “memory_limit”. Valid for single file upload form. May be used
* as MAX_FILE_SIZE hidden input or to inform user about max allowed file size.
* RULE memory_limit > post_max_size > upload_max_filesize
* http://php.net/manual/en/ini.core.php : 128M > 8M > 2M
* Sets max size of post data allowed. This setting also affects file upload.
* To upload large files, this value must be larger than upload_max_filesize.
* If memory limit is enabled by your configure script, memory_limit also
* affects file uploading. Generally speaking, memory_limit should be larger
* than post_max_size. When an integer is used, the value is measured in bytes.
* Shorthand notation, as described in this FAQ, may also be used. If the size
* of post data is greater than post_max_size, the $_POST and $_FILES
* superglobals are empty. This can be tracked in various ways, e.g. by passing
* the $_GET variable to the script processing the data, i.e.
* , and then checking
* if $_GET[‘processed’] is set.
* memory_limit > post_max_size > upload_max_filesize
* @author Paul Melekhov edited by lostinscope
* @return int Max file size in bytes
*/
function detectMaxUploadFileSize(){
/**
* Converts shorthands like “2M” or “512K” to bytes
*
* @param $size
* @return mixed
*/
$normalize = function($size) {
if (preg_match(‘/^([\d\.]+)([KMG])$/i’, $size, $match)) {
$pos = array_search($match[2], array(“K”, “M”, “G”));
if ($pos !== false) {
$size = $match[1] * pow(1024, $pos + 1);
}
}
return $size;
};
$max_upload = $normalize(ini_get(‘upload_max_filesize’));

$max_post = (ini_get(‘post_max_size’) == 0) ?
function(){throw new Exception(‘Check Your php.ini settings’);}
: $normalize(ini_get(‘post_max_size’));

$memory_limit = (ini_get(‘memory_limit’) == -1) ?
$max_post : $normalize(ini_get(‘memory_limit’));

if($memory_limit < $max_post || $memory_limit < $max_upload)
return $memory_limit;

if($max_post < $max_upload)
return $max_post;

$maxFileSize = min($max_upload, $max_post, $memory_limit);
return $maxFileSize;
}

Linesh Jose February 9, 2013 at 5:59 pm

Thanks for this snippet 🙂

Nelson Pérez March 18, 2013 at 3:08 am

Brother, thanks the peace of Christ for you, thanks for info 🙂

quesst April 16, 2013 at 4:41 pm

he is from china. he does not know christ. you are stupid.

Comments on this entry are closed.

Previous post:

Next post: