PHP upload file size limit issues

Source: Internet
Author: User
Tags php file upload ftp client

In the php file upload operation, you need to know how to control the upload file size settings, and file size is constrained by a variety of factors, now summarized as follows:

1,php.ini:upload_max_filesize The maximum size of the uploaded files. The default value is 2M.

2.Php.ini:memory_limit This directive sets the maximum number of bytes of memory that a script can request, with a default value of 8M. If you do not need any memory restrictions, you must set it to-1. If there is not enough memory, an error may occur: Fatal error:allowed memory size of X bytes exhausted (tried to allocate Y bytes)

3,php.ini:post_max_size Set the maximum allowable size of post data. This setting also affects file uploads. To upload a large file, the value must be greater than upload_max_filesize.

4,php.ini:max_execution_time =; Maximum execution time of each script, in seconds

5,php.ini:max_input_time =; Maximum amount of time each script may spend parsing request data

6. If you use a MySQL blob for binary file storage, you need to set the my.ini:max_allowed_packet=xxm

7, httpd.conf in Apache there is an option is limitrequestbody, this option can limit the user sent HTTP request content. This option can be used in. htaccess or httpd.conf, and if used within the httpd.conf, it can be used in the VirtualHost or directory attribute settings, respectively. The Limitrequestbody setting value is between 0 (unrestricted) and 2147483647 (2GB). For example, to set the upload limit to 100K in the directory d:/appserv/www, you can add the following statement to the. htaccess or httpd.conf:

Limitrequestbody 1024000000 Options Indexes followsymlinks multiviews execcgi allowoverride all Order allow,deny allow fro M all

Limitrequestbody 1024000000Options Indexes followsymlinks multiviews execcgiallowoverride allorder allow,denyallow From all

If it is set through the. htaccess, it will take effect immediately after the file is stored, and if it is set by httpd.conf, it needs to restart Apache. PHP about the File Upload section, specifically refers to the form hidden fields: max_file_size, meaning to receive the maximum size of the file. The examples given in the documentation are as follows:

<form enctype= "Multipart/form-data" action= "_url_" method= "POST" > <input type= "hidden" name= "max_file_size" Value= "30000" > Send this file: <input name= "UserFile" type= "file" > <input type= "Submit" value= "Send File" > form>

Send This file:

Set max_file_size = 30000here, expecting a possibility that the browser can make a predetermined decision before transmitting the file, and if the file size is greater than 30000 bytes, the actual post action is not performed. That is, instead of sending the file content to the server, it alerts the user directly to the "file you are trying to upload exceeds 30000 bytes". This is really a great proposition, but in reality it is temporarily impossible to achieve. It is not because this restriction can be "simply bypassed", but both IE and Firefox do not support this feature in both mainstream browsers. This proposal for PHP has not yet been adopted.

Max_file_size also has a useful: the background PHP will determine whether the received file size is greater than this value, if exceeded, $_files[' thisfile ' [' Error '] will be set to Upload_err_form_size (2), At the same time discard save temporary file, will $_files[' thisfile ' [' Size '] set 0. This example, no problem, normal performance, when I tried to upload a file of more than 40 K, the PHP program reported "file more than max_file_size." But what if we reduced the max_file_size from 30000 to 1000 in the form? Upload 800 bytes of file, normal; upload 40K file, PHP report file is too large, also normal; upload 3,000 bytes of file, PHP did not report the error, it saved the file successfully! Unexpected! The question is on this part of the code that main/rfc1867.c to determine whether the file is super-long. Each time PHP reads the contents of Fillunit bytes from buffer, it first determines whether the read-only length of content (total_bytes) is greater than max_file_size, and then increases the length of content (Total_bytes) that has been read. As a result, there will be at most fillunit bytes of error between the expected results and fillunit=1024*5=5k. (Click Bug to learn more) This means that when max_file_size<5k, uploading a file larger than max_file_size, but less than 5K is no problem. Of course, because this setting is easily bypassed, server-side programming should not rely on max_file_size. Also, the 5K is a very small value and has no effect on most forms of uploading files. PHP post_max_size,upload_max_filesize, max_file_size settings, and on the client to the server side of the traffic size independent. The Apache server receives a request from the client for a length of not more than Limitrequestbody bytes and then transmits it to the PHP module, which then decides whether to save it as a temporary file, set the $_files global variable, and hand it over to the script for further processing. This Apache limitrequestbody option default = 0, the maximum number of bytes allowed for the request body is 2G (Linux + Apache) Finally, it is also important to note that the HTML itself can post data is also limited, not more than 2G. FTP client has 2GB boundary limit for file offset pointer, the FTP server side or client that is not compiled with the special compile flag is not supported for files larger than 2GB, regardless of the FS. I do not know if PHP will also have this situation.

PHP upload File size limit issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.