Original source: http://evol1216.blog.163.com/blog/static/13019958020106783623528/
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_filesizeThe maximum size of the uploaded file. The default value is 2M.
2, Php.ini:memory_limitThis 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) (General Import Database, if the database is too large, it will be error, change this It can be)
3, Php.ini:post_max_sizeSets the maximum size allowed for 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 are using a MySQL blob for binary file storage, you need to setMY.INI:MAX_ALLOWED_PACKET=XXM
7, httpd.conf
InApacheThere is an option insideLimitrequestbody, this option restricts the content of HTTP requests sent by the user. 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 is set at a value of 0 (No limit) to 2147483647 (2GB)。
For example, to set the directory D:/appserv/wwwUploadLimit to 100K, you can add the following statement in. htaccess or httpd.conf:
- Limitrequestbody 1024000000
- Options Indexes followsymlinks multiviews execcgi
- AllowOverride All
- Order Allow,deny
- Allow 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 >
Set max_file_size = 30000 here, expecting a possibility that the browser can make a predetermined decision before transferring the file, and if the file size is larger 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. not because this restriction can be "simply bypassed", instead, IE and Firefox are the two main browsers does not support this feature . 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 on the 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.
The default value of this Apache limitrequestbody option = 0, the maximum number of bytes allowed for the request body is 2G (Linux + Apache)
Finally, it is important to note that:
The HTML itself can be post data is also limited, not more than 2G.
The FTP client has a 2GB boundary limit for the file offset pointer, no FTP server side or client that is compiled with a specially compiled flag, and files larger than 2GB are not supported in any FS. I do not know if PHP will also have this situation.