Configure php. ini to implement php file Upload

Source: Internet
Author: User
Tags http file upload php file upload
We will introduce you to php. how to configure the php file upload function in the INI file involves some important options and restrictions on the size of php files to be uploaded, for more information about PHP website development, see yesterday. PHP Tutorial for configuring session in ini. today we will continue to share the key points of PHP when using php to implement the file upload function. ini configuration.

When talking about the file upload configuration in php. ini, I mentioned the php file upload function code example tutorials and Jquery AjaxUpload code examples. The php file upload function configuration mainly involves the upload_tmp_dir, upload_max_filesize, post_max_size, and other options in the php. ini configuration file.

Configuration options of file upload in php. ini

Open the php. ini configuration File and find File Uploads.

File_uploads = On

HTTP File upload is allowed by default. this option cannot be set to OFF.

Upload_tmp_dir =

The default value is null. this option is easy to forget when you manually configure the PHP runtime environment. if this option is not configured, the file upload function cannot be implemented, this option sets the temporary directory for storing files during File upload. you must assign a value to this option, for example, upload_tmp_dir = '/leapsoulcn', which indicates that there is a leapsoulcn directory under the C directory, like session configuration, if you are in a linux environment, you must grant this directory write permission.

  How do I upload large files larger than 8 MB?

Uploading large files involves configuring the upload_max_filesize and post_max_size options.

Php. the default file upload size in the ini configuration file is 2 MB. a common mistake for php beginners is to set the form area with the maximum file size when writing the file upload function, that is, the maximum value allowed to upload files. The value of max_file_size (hidden value range) specifies the size of the uploaded files. In fact, this value can be bypassed by others. for security reasons, it is best to use php. configure the upload_max_filesize option in the ini configuration file to set the file upload size.

The default upload_max_filesize = 2 M, that is, the file size is 2 M. If you want to upload files larger than 8 M, such as 20 M, you must set upload_max_filesize = 20 M.

However, if you set upload_max_filesize = 20 m, you still cannot upload large files. you must modify php. the post_max_size option in the ini configuration file, which indicates the maximum length of bytes allowed for POST data. the default value is 8 MB. If the POST data exceeds the limit, $ _ POST and $ _ FILES will be empty. To upload a large file, you must set the option value to be greater than the value of the upload_max_filesize command. I usually set the value of upload_max_filesize to be equal to that of post_max_size. In addition, if the memory limit is enabled, this value should be smaller than the value of the memory_limit option.

Other precautions for file Upload

When uploading a large file, you may feel that the upload speed is slow. if it exceeds a certain period of time, an error occurs that the script execution exceeds 30 seconds. in the ini configuration file, the max_execution_time configuration option is incorrect. it indicates the maximum allowed execution time (in seconds) of each script, and 0 indicates no limit. You can adjust the value of max_execution_time appropriately. it is not recommended to set it to 0.

So far, in php. the PHP Tutorial for configuring file upload options in the ini configuration file is complete. the file upload function can be implemented through the above steps, practices and learning, and the PHP program.

Below are some supplements:


This article describes how to configure php. ini to implement the php file upload function. It involves the upload_tmp_dir, upload_max_filesize, post_max_size, and other options in the php. ini configuration file. these options are critical to the success or failure of file upload. Let's take the Windows version of php.5.3.5 as an example.

Configuration options of file upload in php. ini
Use a text tool (EditPlus recommended) to open the php. ini configuration File and find File Uploads. the following three options are available in this area:

File_uploads = On

Whether to allow HTTP File upload. The default value is On. this option cannot be set to Off.

Upload_tmp_dir =

Temporary directory for file upload. If this parameter is not specified, PHP uses the default temporary directory. This option is empty by default. this option is easy to forget when you manually configure the PHP runtime environment. if this option is not configured, the file upload function cannot be implemented. you must assign a value to this option, for example, upload_tmp_dir = "d:/fileuploadtmp" indicates that there is a fileuploadtmp directory under the d directory, and the read and write permissions are granted to this directory.

Upload_max_filesize = 2 M

The maximum size of the uploaded file. The default value of this option is 2 M, that is, the file size is 2 M. If you want to upload a 50 M file, you must set upload_max_filesize = 50 M.
However, if you only set upload_max_filesize = 50 M, you still cannot upload large files. you must modify the post_max_size option in the php. ini file.

Continue to search for Data Handling in php. ini. there is one option in this region:

Post_max_size = 8 M

The maximum value that can be received by posting a form to PHP, including all values in the form. The default value is 8 MB. If the POST data exceeds the limit, $ _ POST and $ _ FILES will be empty.
To upload a large file, you must set the option value to be greater than the value of the upload_max_filesize option. for example, if you set upload_max_filesize = 50 M, you can set post_max_size = 100 M.
In addition, if the memory limit is enabled, this value should be smaller than the value of the memory_limit option.

Continue to search for Resource Limits in php. ini. There are three options in this region:

Max_execution_time = 30

The maximum running time (in seconds) of each PHP page. the default value is 30 seconds. When we upload a large file, such as a 50 m file, it may take several minutes to complete the upload, but the php default page's longest execution time is 30 seconds, more than 30 seconds, the script is stopped, and thus the web page cannot be opened. Therefore, we can set a larger value, such as max_execution_time = 600. If it is set to 0, there is no time limit.

Max_input_time = 60

The time (in seconds) used by each PHP script to parse the request data. the default value is 60 seconds. When uploading a large file, you can set this value to a greater value. If it is set to 0, there is no time limit.

Memory_limit = 128 M

This option is used to set the maximum memory space that can be applied by a single PHP script. This helps prevent poorly written scripts from consuming the available memory on the light server. If you do not need any memory limit, set it to-1.
Php5.2.0 and earlier versions are 8 MB by default; php.5.2.0 and 16 MB by default. Php versions later than 5.2.0 are 128 M by default;

Example of the php. ini file upload configuration function
Assume that you want to upload a 50 m large file. Configure php. ini as follows:
File_uploads = On
Upload_tmp_dir = "d:/fileuploadtmp"
Upload_max_filesize = 50 M
Post_max_size = 100 M
Max_execution_time= 600
Max_input_time= 600
Memory_limit = 128 M

Tip: you need to keep memory_limit> post_max_size> upload_max_filesize

This example is for reference only. you can adjust it as needed.

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.