PHP Upload large file failed solution

Source: Internet
Author: User
Tags file upload ini memory usage php file variable

Yesterday, a user to do PHP development asked me why upload large files always fail, but upload small files there is no problem. The author hurry also do not know what the reason, hence the online search to help its analysis of reasons, see a very good about PHP upload large file failure of the various causes of the summary and various solutions, so turn to share with you.

The following are a variety of reasons and solutions

(1) The temporary directory where files are uploaded must be open and be writable by the owner of the PHP process. If not specified, PHP uses the system default value.

In the php.ini file, Upload_tmp_dir is used to describe the temporary directory where the files are uploaded by PHP, and to upload files, make sure that the server does not close the temporary files and write permissions on the folder.

(2) The value of the max_execution_time must be large enough. Variable Max_execution_time sets the time, in seconds, that PHP waits for the script to finish before the script is forced to terminate. This variable is useful when the script enters an infinite loop state. However, when there is a legitimate activity that takes a long time to complete (such as uploading a large file), this feature can also cause the operation to fail. In this case, you must consider increasing the value of this variable to avoid PHP shutting down the script when the script is performing some important process, for example, set to 90 seconds.

Max_execution_time = 90

Note that there is also a set_time_limit function in the PHP function, and the effect of this setting is the same, the difference is that the above is set in the php.ini file, and Set_time_limit is written in the PHP file. So you can also use Set_time_limit to set the maximum execution time of the program in the page, such as Unrestricted: Set_time_limit (0);

(3) File_uploads = On the default value is on, meaning that file uploads are allowed over HTTP, this option cannot be set to OFF.

(4) Upload_max_filesize = 2M Set the maximum size of the file upload, Default file upload size in php.ini profile for 2m,php Beginners An error is to make the file upload function by setting the maximum size of the upload file form area, that is, the maximum allowable upload file, Max_file_ Size (hidden range) value to specify the size of the upload file, in fact, other people can bypass this value, so security, it is best to configure the Upload_max_filesize option in the php.ini configuration file, set the file upload size. Default upload_max_filesize = 2M, that is, the size of the file upload is 2M, if you want to upload more than 8M of files, such as 20M, you must set upload_max_filesize = 20M.

(5) Post_max_size This value must also be large enough. This variable is also a variable associated with the form submission, which restricts the maximum amount of data that the PHP program can receive when the client makes a form submission through the Post method. In general, you can set the value of this value slightly larger than the upload_max_filesize, such as to upload a 20MB file, this value could be set to 21MB.

(6) Max_input_time This variable can be used in seconds to limit the time it takes to receive data by post, get, and put. If your application is running in a slow network environment, you need to increase this value to increase the time required to receive data, such as setting this value to 90 seconds.

Max_input_time = 90

(7) Memory_limit must also be large enough to prevent running scripts from using the system's available memory in large quantities, and PHP allows you to define memory usage limits. Specifies the maximum amount of memory that a single script program can use by Memory_limit variables. The value of the variable memory_limit should be appropriately greater than the Post_max_size value.

(8) In addition if your host is Nginx operating system, if the above operation is not possible, remember to Nginx in the configuration file plus client_max_body_size 20m; Such a sentence, that is to say that the maximum allowed to upload 20MB, specific according to their own circumstances. The location of my nginx configuration file is in/usr/local/nginx/conf/nginx.conf.



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: info-contact@alibabacloud.com 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.