Php cannot upload large files perfect solution

Source: Internet
Author: User
Tags php form

Php cannot upload large files perfect solution

Php. ini: a perfect solution for uploading large files

1. Open php. ini (not to mention opening the method, Baidu has a lot)

2. Search for post_max_size

Maximum form submission value. This parameter is not used to restrict the size of a single file to be uploaded, but is used to restrict the submitted data of the entire form.

The default value is 8 MB and is set to the value you need. We recommend that you set this parameter to a value larger than upload_max_filesize.

3. Search for File Uploads

Whether to allow file upload over http. Check whether file_uploads = on

4. Find upload_tmp_dir.

The file is uploaded to the place where the temporary file is stored on the server. If it is not specified, the default Temporary Folder will be used.

If the system reports an error message indicating "XXX temporary directory XXX", you need to set a valid directory for this directory.

5. Find upload_max_filesize

Maximum file size allowed to be uploaded. The default value is 2 MB. Set this parameter to the value you need. We recommend that you do not set this parameter to exceed the value of post_max_size because it is controlled by the value of post_max_size (even if the value of upload_max_filesize is, if post_max_size is set to 2 MB, files larger than 2 MB cannot be uploaded because it is controlled by the value of post_max_size)

6. If you want to upload a file larger than 8 m, you also need to set the following parameters:

Query max_execution_time = 600; maximum time (in seconds) for running each PHP page. The default value is 30 seconds.

Max_input_time = 600; maximum time required for receiving data on each PHP page. The default value is 60 seconds.

Memory_limit = 8 M; maximum memory required for each PHP page. The default value is 8 M.

Perfect solution for uploading large files using nginx

1. client_body_buffer_size should be set as large as possible. This is based on the speed. If the size of the file to be uploaded is too small, it will be too slow to write to the disk.

2. The client_body_temp_path must have the write permission. This is an obvious error. Just correct it.

3. client_max_body_size sets the maximum value of the uploaded file. This is based on security considerations. We believe that normal users will not or will not upload too many files.

It can be set to client_max_body_size 100 m, or set this value according to your own business.

Example

Nginx configuration.

The Code is as follows:  

Cd/export/servers/nginx/conf/nginx. conf.

Location /{

Root html;

Index index.html index.htm;

Client_max_body_size 1000 m;

}

When the client_max_body_size field is added, it cannot be restarted. Later, the sub-configuration file was found in the total configuration file:

The Code is as follows:  

Sendfileon;
# Tcp_nopush on;

# Keepalive_timeout 0;
Keepalive_timeout 65;

# Gzip on;
Include domains /*;
# Include domains/chat. local;
# Include domains/chat.erp.com;
# Include domains/support.chat.com;
# Include douains/chat.com;

Server {
Listen 80;
Server_name localhost;

The sub-configuration file is found and modified in the sub-configuration file. The configuration file is as follows:

The Code is as follows:  
Server
{
Listen 80;
Server_name chat.erp.360buy.com;
# Access_log/export/servers/nginx/logs/chat.erp.360buy.com;
Location /{
Proxy_pass http: // tomcat;
Client_max_body_size 1000 m;
}
}

Restart with/export/servers/nginx/sbin/nginx-s reload to solve the problem of limited file size.

I will share my solution process and hope to help you.

If the problem persists, we need to check whether php form settings are correct.

This code is divided into two files, one is upload.html and the other is upload. php.

Upload.html

The Code is as follows:  
<Form enctype = "multipart/form-data" action = "upload. php" method = "post">
<Input type = "hidden" name = "max_file_size" value = "100000">
<Input name = "userfile" type = "file">
<Input type = "submit" value = "Upload File">
</Form>

Where,

Note:

This is a tag. to upload a file, you must specify it as multipart/form-data. Otherwise, the server will not know what to do.

The Value is the hidden Value range of the form option MAX_FILE_SIZE in the file upload.html. You can set the Value to limit the size of the uploaded file.

The value of MAX_FILE_SIZE is only a suggestion for the browser. In fact, it can be simply bypassed. Therefore, do not forward browser restrictions to this value. In fact, the maximum size of uploaded files in PHP settings will not expire. But it is best to add MAX_FILE_SIZE to the form, because it can avoid the trouble of finding the file too big after you wait for the upload of a large file.

Upload. php

The Code is as follows:  
$ F = & $ HTTP_POST_FILES ['myfile'];
$ Dest_dir = 'uploads'; // sets the upload directory.
$ Dest = $ dest_dir. '/'. date ("ymd"). "_". $ f ['name']; // set the file name to date and add the file name to avoid duplication.
$ R = move_uploaded_file ($ f ['tmp _ name'], $ dest );
Chmod ($ dest, 0755); // sets the attributes of the uploaded file
Or
<? Copy ($ _ FILES [MyFile] [tmp_name], $ _ FILES [MyFile] [name]);?>

In the preceding example, the content of the $ _ FILES array is as follows. Assume that the name of the file upload field is userfile)

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.