Change the maximum upload file size

Viewed 1207653

I have a website hosted on a PC I have no access to. I have an upload form allowing people to upload mp3 files up to 30MB big. My server side script is done in PHP.

Every time I try and upload a file, I receive an error claiming that the file exceeds the maximum size allowed, so I need to increase the size. My research on the web suggested changing the .htaccess file which I do not have access to, so that won't work. Others suggested that I should add a custom php.ini file to my root which did not work. Any other suggestions?

23 Answers

You need to set the value of upload_max_filesize and post_max_size in your php.ini :

; Maximum allowed size for uploaded files.
upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize
post_max_size = 40M

After modifying php.ini file(s), you need to restart your HTTP server to use the new configuration.

If you can't change your php.ini, 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.

See the Description of core php.ini directives.

You can change it via an .htaccess file.

.htaccess files are stored in the same directory as your .php files are. They modify configuration for that folder and all sub-folders. You simply use them by creating an .htaccess file in the directory of your choice (or modify it if present).

The following should enable you to increase your upload limit (if the server provider allows PHP config changes via .htaccess).

php_value upload_max_filesize 40M
php_value post_max_size 42M

With WAMP it's all pretty easy

WAMP Icon > PHP > PHP Settings > upload_max_filesize = nM > n = (2M, 4M, 8M, 16M, 32M, 64M, 128M, 256M, 512M, or Choose (custom)).

Service(s) reload automatically.

But, if you truly have no access to the server, you might want to explore writing a chunking API.

Here is an image on how to do it.

enter image description here

As changing globally is somewhat risky, I was trying to increase max upload value for a single script big_file_upload.php. For some reason ini_set didn't help. After some reasearch I've come up with this. Put it in .htaccess (unless name changed via AccessFileName)

<If "%{REQUEST_URI} == '/subfolder/big_file_upload.php'" >
php_value upload_max_filesize 200M
php_value post_max_size 200M
</If>
<Else>
php_value upload_max_filesize 1M
php_value post_max_size 1M
</Else>

Worked for me.

If you edited the right php.ini file, restarted Apache or Nginx and still doesn't work, then you have to restart php-fpm too:

sudo service php-fpm restart 

I also had this issue, and fixed it by setting this setting in /etc/nginx/nginx.conf

client_max_body_size 0;

0, as in unlimited.

And also, if you have a reverse proxy running with nginx, that server should also have this setting (This is what threw me off here)

Existing answers all have partial solutions so here is the compiled answer:

Solution 1: Edit .htaccess file Suitable for Apache servers

php_value upload_max_filesize 128M
php_value post_max_size 132M
php_value memory_limit 256M
php_value max_execution_time 300
php_value max_input_time 300

Solution 2: Edit wp-config.php file Suitable for Wordpress application

@ini_set( 'upload_max_filesize' , '128M' );
@ini_set( 'post_max_size', '132M');
@ini_set( 'memory_limit', '256M' );
@ini_set( 'max_execution_time', '300' );
@ini_set( 'max_input_time', '300' );

Solution 3: Edit php.ini file Suitable for nginx servers or where php.ini is modifiable

upload_max_filesize = 128M
post_max_size = 132M
memory_limit = 256M
max_execution_time = 300
max_input_time = 300

Optional: Nginx Server
For nginx increase maximum file upload size limit (default 1MB) by adding client_max_body_size 128M; directive in http or server block.]

Important Explanation of the settings:

  • upload_max_filesize – set this to a value > than your file size
  • post_max_size – set this to a value > than your upload_max_filesize because overall size of the posted fields may be higher than the filesize
  • memory_limit – set this to a value > than your file size because it limits the maximum amount of memory a script may consume
  • max_execution_time – Maximum execution time of each script, in seconds. Set this to 0 (infinite) if your script requires long time to process file
  • max_input_time - Maximum amount of time each script may spend parsing request data. Default is 60 seconds. Set this to -1 if scripts requires more time

And finally don't forget to restart your server. Apply below what is suitable for you:

# if php-fpm is used for processing php
sudo service php7.4-fpm restart

# for nginx
sudo service nginx restart

# for apache
sudo service httpd restart
  1. Open the php.ini file.

  2. Search keyword like upload_max_filesize in php.ini.

  3. Then change the size of file.

    upload_max_filesize = 400M

  4. Need to change the max post value.

    post_max_size = 400M

After spending hours, I went through almost all the posts but no luck. And finally resolved the issue with these steps. This might be weird solution but worked for me.

Step 1: find php.ini file in /etc folder or / folder by running below cmd:

grep -rl "post_max_size" | xargs ls -lrth

Here I have used the post_max_size keyword to search the "php.ini" file in /etc folder ,but in some systems you can find this on /var/www/html or /var/www/wordpress folders.

We have multiple posts on the internet as if the php.ini file is not present in the WordPress folder then you can create but in my case that doesn't work.

Step 2:

Edit php.ini file and change the value like this.

post_max_size = 100M   
upload_max_filesize = 100M

In the above you can set any value as per your requirement.

Step 3:

Restart the httpd or Nginx or apache and PHP service, or as per setup, you can restart the web service.

For me httpd and php-fpm service restart worked in centos 8 :

service httpd restart
service php-fpm restart

you have to find the where is php installed you will see the php.ini file

just open that file into any editor and replace th value

max_file_upload : 2M

First Option: Use this option in case you have permission to edit the php.ini file.

The upload_max_filesize and the post_max_size settings in the php.ini need to changed to support larger files than the php default allowed size.

set upload_max_filesize to the maximum size of file which need to be uploaded.

post_max_size should be set to greater than upload_max_filesize to handle the file and the size of form post variables part of file upload form. In case multiple files are uploaded in the file upload form at a time, the post_max_size should be set to (upload_max_filesize * no of files)+ size of post variables.

upload_max_filesize=30M
post_max_size=31M

No need to update memory_limit and max_execution_time php settings, in case file is just moved using the move_uploaded_file to the final path in file upload php script. But if the file upload script is processing the file or reading the file to a variable or doing any other processing which use memory and time etc, memory_limit and max_execution_time should be set.

Set memory_limit to amount of memory needed for the php script to run and max_execution_time to the time in seconds required to run the script.

memory_limit = 100M
max_execution_time = 120

Restart the webserver after the php.ini is changed for it to take effect.

Second Option: Use this option in case you do not have permission to update the global php.ini settings or to improve security.

Copy the upload php scripts to a new Child folder say "Upload" and create a file ".user.ini" in the new folder. Also make sure to update the file upload form action attribute to post to the new Script under the "Upload" Folder.

Add below settings to the newly created file. ".user.ini".

upload_max_filesize=30M
post_max_size=31M

;below are optional
memory_limit = 100M
max_execution_time = 120

"user_ini.filename" php.ini setting can updated to give the user setting file another name.

This option improves the security as the upload_max_filesize, post_max_size,memory_limit, max_execution_time are changed only for the scripts in the new folder and will not impact php settings for scripts in other folders and prevents any unwanted resource utilization by bad scripts.

Please refer below links for more information on ".user.ini" settings

https://www.php.net/manual/en/configuration.changes.modes.php

https://www.php.net/manual/en/ini.list.php

https://www.php.net/manual/en/configuration.file.per-user.php

Restart the webserver for the new .user.ini changes to take effect.

Note:

The memory_limit and max_execution_time setting can also be set in the php upload script using ini_set and set_time_limit function instead of updating php.ini file .

If this option is used, not need to update the memory_limit and max_execution_time setting in the ini file.

Add below to the top of the php file upload script

ini_set('memory_limit', '100M');
set_time_limit(120);
Related