PHP cannot upload large files

Source: Internet
Author: User
Tags parse error php file upload

When studying PHP File Upload today, we found that the file cannot be uploaded successfully. In fact, you only need to make some settings for PHP.

After searching, you can find out how to adjust the size of upload_max_filesize and post_max_size. But there is a more detailed article on the InternetArticle:

Here is an extra error: $ _ FILES ['userfile'] ['error'] cannot be detected. If the uploaded file is too large and exceeds the post_max_size set by PHP, another problem occurs. the following will crash, please pay attention to post_max_size: manual explanation
Set post
The maximum size allowed by data. This setting also affects file upload. To upload a large file, the value must be greater than upload_max_filesize.

If the memory limit is activated in the configuration script, memory_limit also affects file upload. Generally, memory_limit should be larger than post_max_size.
Large.

When usingIntegerType, the value is measured in bytes. You can also use simplified symbols. For more information, see this FAQ.

If the size of post data is larger than post_max_size $ _ post and $ _ FILES superglobals
It will be empty. This can be proved in multiple ways, for example, passing the $ _ Get variable to the script to process data, that is, <form
Action = "Edit. php? Processed = 1 ">, and then check whether $ _ Get ['processed'] is set.

 

How PHP uploads large files: principles and implementation of PHP File Upload
Upload using PHP file functions

 

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

Upload.html
<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

enctype = "multipart/form-Data"……> 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 in file upload.html
max_file_size. 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 value of the uploaded file in PHP
is not invalid. But it is best to add
max_file_size to the form, because it can avoid the trouble of finding the file too large after you wait for the upload of a large file.
upload. php
$ F = & $ http_post_files ['myfile'];
$ dest_dir = 'uploads'; // sets the upload directory.
$ DEST = $ dest_dir. '/'. date ("ymd "). "_". $ f ['name']; // set the file name to the date plus the file name to avoid duplication
$ r = move_uploaded_file ($ f ['tmp _ name'], $ DEST );
chmod ($ DEST,
0755); // set the attributes of the uploaded file
or

the content of the
$ _ FILES array in the preceding example is as follows. Assume that the name of the file upload field is userfile (the name can be arbitrarily named)

$ _ FILES ['userfile'] ['name'] the original name of the client machine file.
$ _ FILES ['userfile'] ['type'] File
MIME type, which must be supported by the browser, for example, "image/GIF ".
$ _ FILES ['userfile'] ['SIZE']
Size of the uploaded file, in bytes.
$ _ FILES ['userfile'] ['tmp _ name']
Temporary File Name stored on the server after the file is uploaded.
$ _ FILES ['userfile'] ['error'] Error Code related to the File Upload
Value: 0;
No error occurred. The file is uploaded successfully.
Value: 1; the uploaded file exceeds the limit of the upload_max_filesize option in PHP. ini.
Value: 2;
The size of the uploaded file exceeds the value specified by the max_file_size option in the HTML form.
Value: 3; only part of the file is uploaded.
Value: 4;
No files are uploaded.
[Edit] parameters involved in PHP File Upload
PHP's default upload limit is 2 MB. to upload files that exceed this setting, you need to adjust some parameters such as PHP and Apache.
Next, we will briefly introduce some parameters involved in PHP file upload:

File_uploads
Whether to allow file upload over HTTP. On is enabled by default.

Upload_tmp_dir
Upload_tmp_dir indicates the temporary directory where PHP files are uploaded. to upload files, make sure the server does not have the permission to close the temporary files and write the files to the folder. If not specified, PHP uses the system default value.

Upload_max_filesize
Maximum file size that can be uploaded. The default value is 2 MB.

Post_max_size
Controls the maximum data size that PHP can receive in a form submission using the POST method. If you want to use the PHP file upload function, you need to change this value to a value greater than upload_max_filesize.

Max_input_time
The time for receiving data through post, get, and put is limited in seconds. If the applicationProgramWhen the running environment is on the low-speed link, you need to add this value to adapt to the more time required to receive data.

Memory_limit
To avoid using a large amount of available memory by running scripts, PHP allows you to define the memory usage limit. Use the memory_limit variable to specify the maximum memory capacity variable memory_limit that can be used by a single script program. The value of memory_limit should be greater than the value of post_max_size.

Max_execution_time
Max_execution_time
Set the time for PHP to wait for the script to be executed before force termination. The time is calculated in seconds. This variable is useful when the script enters an infinite loop state. However, it takes a long time to complete
For example, uploading large files. In this case, you must consider increasing the value of this variable to avoid PHP when the script is executing some important processes.
Close the script

For Linux Hosts, PHP. conf may exist in/etc/httpd/CONF. d/access. CONF /.
File, this file may solve some system file size restrictions
[Edit] Use discuz to upload large PHP files

The attachment function of the Forum was originally designed not for file management. Due to server configuration, PHP, network, and other factors, uploading files through the Forum is not a good solution.

If you really need to improve the uploading conditions of Forum attachments, you can try to set the parameters mentioned above in PHP. ini to meet the needs of uploading large files. At the same time, do not forget to set attachments in the background of the Forum.

There are two main parts of the Forum to limit the size of attachment uploads, from high to low:

Post-related-attachment type size
User Group-attachment related
At the same time, the following provides configuration guidance from friends who have successfully uploaded large attachments via HTTP. Of course, due to the different server configurations and network conditions, this does not necessarily apply to your situation. You may need to modify it in many places:

Open PHP. ini,

Parameter settings
Whether file_uploads on allows file upload over HTTP. On is enabled by default.
 
Upload_tmp_dir-upload the file to the place where the temporary file is stored on the server. If this parameter is not specified, the default Temporary Folder will be used.
 
Upload_max_filesize 8 m wangwen business, that is, the maximum file size allowed to be uploaded. The default value is 2 MB.
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.
Description
 
Generally, after the preceding four parameters are set, uploading a file <= 8 m is not a problem under normal network conditions.
 
However, if you want to upload a large file larger than 8 Mb, you can only set the above four items. Unless your network has a high upload speed of 100 Mb/s, you must set the following parameters.
 

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 consumed by each PHP page. The default value is 8 m.

After modifying the preceding parameters, you can upload a large volume of files as permitted by the network.

[Edit] common error types for uploading Forum files (keep summing up ...)
Warning: Unable to open' \ PhP2 'for reading:
Invalid argument in E: \ User \ WEB \ larksoft.net \ upload \ upfile. php on line
10
Is the reason for PHP's upload_tmp_dir. The specified directory must be readable and writable.

Parse error: Parse error in c: \ Program Files \ apache
Group \ apache \ htdocs \ mdweb \ ftpfile \ upload. php on line 14
Parse
Errors are generally statements, such as ";", "'", and ")" matching problems.

It's done. in/etc/httpd/CONF. d/PHP. conf, there is
<Files
*. Php>
Setoutputfilter PHP
Setinputfilter PHP
Limitrequestbody
524288
</Files>
Modify the limitrequestbody. This article reposted the network, which is hereby explained!

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.