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

by Yang Yang on February 23, 2010

Share This Article:
Subscribe to Kavoir: blog feed

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.

Share This Article:
Subscribe to Kavoir: blog feed

You should also read:

{ 4 comments… read them below or add one }

starak January 31, 2011 at 7:19 am

Great! Thanx :)

Reply

James Waples February 20, 2011 at 7:28 pm

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

Reply

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.

Reply

Fery Ardiant January 9, 2012 at 3:04 am

Nice Thanks!

Reply

Leave a Comment

Previous post:

Next post: