PHP File Upload Overview

Source: Internet
Author: User

SELF: http://www.neatstudio.com/show-1343-1.shtml

This is an old article. In fact, emotionally speaking, I prefer to introduce it in the recommendation manual. But in fact, when some friends in some PHP groups are asking, they never think of manual translation. Well, the English manual is really uncomfortable, but it is impossible to translate it as soon as it comes out? It is better to take a good look at English when everything is expected. Furthermore, the English in the manual is really simple.
I don't know what your manual is like, but I have such a chapter in my manual:

Chapter 38. handling file uploads
  1. POST method uploads
  2. Error messages explained
  3. Common pitfalls
  4. Uploading multiple files
  5. Put method support

There are five cases in total, and we have made a special introduction to the inability to upload large files. He said this:

PHP is capable of login ing file uploads from any RFC-1867 compliant browser (which between des Netscape Navigator 3 or later, Microsoft Internet Explorer 3 with a patch from Microsoft, or later without a patch ).

Related configurations note:See also the file_uploads, upload_max_filesize, upload_tmp_dir, post_max_size and max_input_timedireves Ves inPHP. ini

PHP also supports put-method file uploads as used by Netscape composer and W3C 'samaya clients. See the put method support for more details.

OK, I know you don't want to read it, so I will not talk about it much. Read the Chinese version. The original Article is http://www.sourcejoy.com/php_dev/php_upload.html.
The PHP file upload function is generally a function encapsulated in advance. You can use encapsulated functions directly, but sometimes you cannot use encapsulated functions, it's really hard to remember PHP's upload-related things. Let's make a summary here for future use.

1. form part
To allow users to upload files, add an upload attribute to the HTML form declaration:
Enctype = 'multipart/form-data'
The form method must be post.
The form option max_file_size hidden field is used to limit the size of the uploaded file. It must be placed before the file form element, in bytes.
For example:
<Form enctype = 'multipart/form-data' id = 'A' name = 'aaa' method = 'post' action = 'xxx. php'>
<Input type = 'did' name = 'max _ file_size 'value = '000000'/>
<Input name = 'upload _ file' type = 'file'/>
</Form>

2. Process uploaded files
During upload, PHP receives an array of information about the file, which can be found in the super Global Array $ _ files.
For example, if the file input box in the form is named upload_file, all information about the file is contained in the array $ _ FILES ['upload _ file.
For example, if the customer uploads an image array named "aaa.jpg", the value is as follows:
Name "p5pp.jpg" name of the file during upload
Type "image/JPEG" file type
Tmp_name "/tmp/phpjksdf" temporary file name on the server
Error upload error Return Value
Size 2045 actual file size

The error in the array above will return different constant values, as shown below:
Upload_err_ OK: no error occurred. The file is uploaded successfully.
The size of the upload_err_ini_size file exceeds the limit of the upload_max_filesize option in PHP. ini.
Upload_err_form_size the size of the uploaded file exceeds the max_file_size option value in the HTML form. You can check the form $ files ['up _ file'] ['SIZE'] in the program for processing.
Only part of the upload_err_partial file is uploaded.
The upload_err_no_file user does not provide any file upload
Upload post-processing example:
If (! Move_uploaded_file ($ _ FILES ['F'] ['tmp _ name'], "uploads /". $ _ FILES ['F'] ['name']. ". jpg ")){
Echo "error ";
}

Function:
Move_uploaded_file: Move the uploaded temporary file to the specified directory.
Example:
Move_uploaded_file ('temporary filename ', 'specify file path ')

Is_uploaded_file: determines whether the file is uploaded through http post.
Example:
If (! Is_uploaded_file ($ _ FILES ['F'] ['tmp _ name']) {
Echo 'invalid ';
}

3. Related Parameters
Parameters in PHP. ini uploaded by PHP:
Whether file_uploads allows file upload. The default value is on

Upload_tmp_dir

Upload_max_filesize: maximum size of the file to be uploaded. The default value is 2 MB.

Post_max_size controls the maximum amount of data that PHP can accept in a form submission using the POST method. If you want to upload data using the PHP file, this value should be larger than upload_max_filesize.

Max_input_time limits the time when data is accepted in post/get/put mode in seconds.

Memory_limit to avoid a large amount of system memory used by running scripts, PHP allows you to define the memory usage limit. You can set this parameter to specify the maximum memory capacity that a single script program can use. It should be greater than the value of post_max_size.

Max_execution_time is used to set the time (in seconds) for PHP to wait for the script to be executed before the script is forcibly terminated. This option can restrict the endless loop script, but this function also causes operation failure when there is a long legal activity (such as uploading large files. In this case, you must consider adding this variable.

4. consider uploading multiple files
You can use the $ _ FILES array to Easily upload multiple files. The $ _ FILES array obtains all file fields in the client form to obtain all files uploaded in the same form.

5. Broke the upload memory limit
Method 1:
Modify the memory_limit value in PHP. ini to a larger value, for example, 64 MB.
Method 2:
Use the Apache rewrite method to dynamically modify the value of memory_limit. First, create a. htaccess file and save it to the current directory of the file upload program. The Code is as follows:
Php_value memory_limit 100 m
Php_value post_max_size 30 m
Php_value upload_max_filesize 30 m
Php_value max_execution_time 300
Php_value max_input_tim300
Php_value display_errors on
-- EOF --
Of course, if you still cannot understand it, then I will have nothing to say.

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.