PHP upload file size limit

Modify PHP INI file

First, we need to modify some php related configurations to complete the size limit when uploading files.

file_uploads = on ;

Whether to allow files to be uploaded via HTTP. The default is ON, that is, ON

upload_max_filesize = 8m ;

The maximum file size allowed to upload. The default is 2M

post_max_size = 8m ;

The maximum value that PHP can receive through form POST, including all values in the form. The default is 8M

If POST data size is larger than post_max_size $_POST and$_ FILES superglobals will be empty

Generally, when the network is normal, it is not a problem to upload < = 8m files after setting the above three parameters. However, if you want to upload large volume files > 8m, setting only the above three items may not work.

max_execution_time = 30 ;

The maximum running time of each PHP page (seconds), which is 30 seconds by default.

After more than 30 seconds, the script stops executing, and the web page cannot be opened

max_input_time = 60 ;

The maximum time required for each PHP page to receive data is 60 seconds by default

memory_limit = 8m ;

The maximum memory consumed by each PHP page is 8M by default

Modify nginx Conf file

Nginx limits the size of uploaded files. The solution is to patch the configuration file corresponding to nginx.

client_max_body_size     50m; //File size limit, default 1m
client_header_timeout    1m; 
client_body_timeout      1m; 
proxy_connect_timeout     60s; 
proxy_read_timeout      1m; 
proxy_send_timeout      1m;

Meaning of each parameter:

client_max_body_size

Limit the size of the request body. If it exceeds the set size, a 413 error is returned.

client_header_timeout

If the timeout of reading request header exceeds the set size, 408 error will be returned.

client_body_timeout

If the timeout of the requested entity exceeds 413, an error is returned.

proxy_connect_timeout

http requests cannot be processed immediately by containers (tomcat, netty, etc.) and are placed in the pending pool of nginx for processing. This parameter is the maximum waiting time. The default is 60 seconds. The official recommended maximum is no more than 75 seconds.

proxy_read_timeout

After the http request is processed by the container (tomcat, netty, etc.), nginx will wait for the processing result, that is, the response returned by the container. This parameter is the server response time, which is 60 seconds by default.

proxy_send_timeout

After the http request is processed by the server, it takes 60 seconds by default to send the data back to Nginx.

nginx error: 413 request entity too large

client_max_body_size is used to set the upper limit of the size of the client Request body. The file to be uploaded is in the body, so this parameter can be indirectly regarded as the limit of the file upload size.

The nginx server determines the size of the body through the content length of the request header. If the upper limit is exceeded, the error code 413 Request Entity Too Large will be returned. Setting this parameter to 0 can cancel the length limit.

Syntax:    client_max_body_size size;
Default:   client_max_body_size 1m;
Context:    http, server, location

client_max_body_size can be set in http, server and location blocks, so we can increase the size of the uploaded package for the domain name or even a request address.

Modify / usr / local / nginx / conf / nginx Conf file, find the client_ max_ body_ Set the following value to the value you want to set.

 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
      # 
      location ~ \.php$ { 
        root      /home/www/admin; 
        fastcgi_pass  127.0.0.1:9000; 
        fastcgi_index index.php; 
        fastcgi_param SCRIPT_FILENAME /home/www/admin$fastcgi_script_name; 
        include    fastcgi_params; 
   
        client_max_body_size 35m;    #The file size uploaded by the client is set to 35M 
        client_body_temp_path /home/www/nginx_temp;    #Set temporary directory 
      } 

Appendix: Nginx has an Upload component:

upload_limit_rate 158k       # Upload rate

As follows:

location /upload {
      upload_pass   /up.php;
      upload_cleanup 400 404 499 500-505;
      #upload_store  /data/app/test.local/upload_tmp;
      upload_store  /tmp;
      upload_store_access user:r;
      client_max_body_size 1024M;
      upload_limit_rate 158k;
      upload_set_form_field "${upload_field_name}_name" $upload_file_name;
      upload_set_form_field "${upload_field_name}_content_type" $upload_content_type;
      upload_set_form_field "${upload_field_name}_path" $upload_tmp_path;
      upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;
      upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;
      upload_pass_form_field "^.*$";
      #upload_pass_form_field "^pid$|^tags$|^categoryid$|^title$|^userid$|^user_id$|^is_original$|^upload_file_name$|^upload_file_content_type$|^upload_file_path$|^upload_file_md5$|^upload_file_size$";
    }

Tags: PHP Nginx

Posted by !jazz on Sun, 15 May 2022 17:03:28 +0300