The reasons of the failure of PHP uploading large files and the _php skills of the corresponding strategies

Source: Internet
Author: User
Tags memory usage

Why uploading large files always fails, but uploading small files is no problem. Small series also cannot its solution, the Internet search for its reasons, finishing an article on the PHP upload big file failure reasons and solutions, share to everyone.

The following are a variety of reasons and solutions:
The 1th situation: The temporary directory where files are uploaded must be open and be writable by the user of the PHP process owner . 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.

The 2nd situation: 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);

The 3rd situation: file_uploads = on defaults toon, meaning to allow file uploads over HTTP, this option cannot be set to OFF.

The 4th situation: upload_max_filesize = 2M Set the maximum size of the file upload, the default file upload size in the php.ini configuration file is 2M, PHP Beginners Easily make a mistake is to write the file upload function by setting the maximum size of the upload file form area, that is, allow the maximum 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 to 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.

The 5th situation: 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.

The 6th situation: max_input_time This variable can limit the time it takes to receive data by post, get, and put in seconds . 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

The 7th situation: 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.

8th: 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.

After reading this article you should probably know the reason for the failure of large file upload, I hope this article can provide some ideas, better to solve similar problems.

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.