5-step explanation of the PHP file downloading function

Source: Internet
Author: User
Tags php file upload
Step 5 describes the php file upload function. 1. the form part allows users to upload files. add an upload attribute to the HTML form declaration: enctype = 'multipart/form-data' the method of the form must be the POST form option MAX_FILE_SIZ.

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:

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. break through 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 --
I read the above article and hope to get something from you.
From the red black guest Union (www.7747.net) original: http://www.7747.net/kf/201009/73557.html

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.