PHP file upload restriction problem

  • 2021-12-19 06:13:46
  • OfStack

PHP large file upload takes up a lot of resources, so it is necessary to limit the upload size. The following are the relevant three parameters:

client_max_body_size upload_max_filesize post_max_size

Three error messages corresponding to the above:

Warning: POST Content-Length of 9663102 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

$_FILES['file']['error']==1

nginx Error: 413 Request Entiry Too Large

client_max_body_size is used to set the upper limit of the size of the client Request body (request body), and the file to be uploaded is in the body body, so this parameter can be indirectly regarded as a limit on the upload size of the file.

The nginx server determines the size of the body body by Content-Length of the request header. Exceeding the set upper limit returns an error code 413 Request Entity Too Large. Setting this parameter to 0 removes 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 uploaded packets for domain names or even one request address.

php Error:

Warning: POST Content-Length of 9663102 bytes exceeds the limit of 8388608 bytes in Unknown on line 0
At this point, the uploaded file size is larger than post_max_size.

php No Warning but Unable to Get Uploaded File

At this time  $_FILES['file']['error']==1 The reason for the error is that the uploaded file is smaller than post_max_size but larger than upload_max_filesize.

Extension of knowledge points:

Solution to the problem of file upload size limitation in PHP and Nginx

For some websites of nginx+php, the uploaded file size will be limited in many aspects, one is the limitation of nginx itself, which limits the size of files uploaded by clients, and the other is the default setting of multiple places in php. ini file.

Therefore, in order to solve the problem of uploading file size limit, many modifications must be made. A few places are sorted out below.

1. Modify the file /usr/local/nginx/conf/nginx. conf, look for client_max_body_size and set the following values to the values you want. For example:


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

Appendix: Nginx has one Upload component:

Upload rate, upload Body size, that is, when uploading files, it may be larger?

client_max_body_size 1024M
upload_limit_rate 158k

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$";
    }

2. Modify php. ini


upload_max_filesize = 8M  
post_max_size = 10M  
memory_limit = 20M 
max_execution_time=300 
file_uploads = On # Allow by default HTTP File upload, this option cannot be set to OFF . 
upload_tmp_dir =/tmp/www

When uploading large files, you will have the feeling of slow upload speed. When the time exceeds 1, you will report the error that the script executes for more than 30 seconds. This is because the max_execution_time configuration option in the php. ini configuration file is at work, which indicates the maximum allowed execution time (seconds) of each script, and 0 indicates no limit. You can adjust the value of max_execution_time appropriately, and it is not recommended to set it to 0.

Summarize

The above is the site to introduce the PHP file upload restrictions, I hope to help you, if you have any questions welcome to leave me a message, this site will reply to you in time!


Related articles: