Avoiding “Max upload file size limit exceeded” error in PHP
There are many reasons and situations which can lead to “Max upload file size limit exceeded” error in PHP.
This article tries to point out the most obvious ones. Take it as a base check-list to analyse your problem.
Contents
The php.ini approach
The most common mistake is an incorrect entry in php.ini
files:
- you can use shorthand bytes notation,
- but you can’t use decimals, because interpreter requires this value to be integer,
- thus,
upload_max_filesize = 500M
is a correct entry wholeupload_max_filesize = 0.5G
rounds to zero! - any additional space (i.e.
upload_max_filesize = 500M
) will also cause interpreter to round value to zero.
And with upload_max_filesize
set to zero you can’t actually upload anything.
Don’t forget about post_max_size
variable:
- some interesting Stack Overflow answer about it,
- an article about increasing Apache’s maximum allowed upload file size.
Script-based approach
If you don’t have access to your php.ini and can’t change value of these variables in there then you’re out of luck. You cannot change these values at run-time — uploads of file larger than the value specified in php.ini
will have failed by the time execution reaches your call to ini_set”.
Thus, this is pointless:
ini_set('post_max_size', '64M');
ini_set('upload_max_filesize', '64M');
Uploading a file as a PHP request itself only no PHP code (including above) is executed because upload fails on value set in php.ini
.
Beside, in recent PHP versions these ini-settings are PHP_INI_PERDIR
only, so you can’t set them in your script. See here for more details.
The .htaccess approach
On certain hostings you may try to set this via .htaccess
file.
Unfortunately, it seems that it doesn’t always work!
On my dev machines setting appropriate value in .htaccess
did the job. On the shared hosting however this setting do not work (error message suggests that maximum size, set in .htaccess is exceeded, so this seems like a weird PHP bug).
If everything else fails…
If you’re stuck and there is no way to go, you may consider showing a nice file upload size exceeded message:
- an article at andrewcurioso.com blog,
- Stack Overflow question with many useful answers and comments.
And of course, most important — don’t forget to restart your server / PHP, for your changes to take effect!