5 Steps to the implementation of PHP file transfer function

Source: Internet
Author: User
Tags php file upload
5 Steps to the implementation of PHP file upload function

1. form part
Allow users to upload files and add an uploaded attribute to the declaration of the HTML form:
enctype = ' Multipart/form-data '
The form's method must be a post
Form Options max_file_size hidden fields are used to limit the size of the upload file, which must be placed in front of the file Form element, in bytes.
Such as:

2. Handling Upload Files
When uploading, PHP receives an array of information about the file, which can be found in the $_files Super Global array.
For example, if the file input box in the form has the name Upload_file, all information about the file is included in the array $_files[' Upload_file '].
Such as: The customer uploaded a "aaa.jpg" image array values are as follows:
Name "P5pp.jpg" when uploading files
Type "Image/jpeg" File types
Tmp_name temporary file name on server side of "/TMP/PHPJKSDF"
Error upload wrong return value
Size 2045 File Actual sizes

The error in the above array will return a different constant value, as follows:
UPLOAD_ERR_OK No error occurred, file upload succeeded
Upload_err_ini_size file size exceeds the value upload_max_filesize option limit in php.ini
The size of the Upload_err_form_size upload file exceeds the Max_file_size option value in the HTML form. You can check the form $files [' up_file '] [' size '] in the program to handle
upload_err_partial files are only partially uploaded
Upload_err_no_file user does not provide any file upload
Specific examples of post-upload processing:
if (!move_uploaded_file ($_files[' f ' [' tmp_name '], "uploads/". $_files[' F ' [' name ']. ". JPG ")) {
echo "Error";
}

Function:
Move_uploaded_file move the uploaded temporary files to the specified directory
Example:
Move_uploaded_file (' Temporary filename ', ' Specify File path ')
Is_uploaded_file to determine if the file was uploaded via HTTP post
Example:
if (!is_uploaded_file ($_files[' f ' [' tmp_name '])) {
echo ' illegal ';
}

3. Related parameters
PHP upload design to the parameters in the php.ini:
File_uploads whether to allow uploading of files, default on
Upload_tmp_dir upload file prevents temporary directory, use system default location unspecified
Upload_max_filesize the maximum allowable size of the uploaded file, default is 2M
Post_max_size controls the maximum amount of data PHP can accept in a form submission using the Post method, which is larger than upload_max_filesize if you want to upload it in PHP.
Max_input_time limits the time to accept data by Post/get/put mode in seconds.
Memory_limit to avoid running scripts that use system memory heavily, PHP allows you to define memory usage limits. Set this parameter to make the maximum memory capacity that a single script can use, which should be greater than the Post_max_size value appropriately
Max_execution_time is used to set the time, in seconds, that PHP waits for the script to complete before forcing the script to terminate. The secondary option restricts the dead loop script, but this feature also causes the operation to fail when there is a long legal activity (such as uploading a large file). In such cases, you must consider increasing this variable.

4. Consider uploading multiple files
Multiple file uploads can be easily achieved with the $_files array. The $_files array can get all the file domain contents of the client form, thus obtaining all the files uploaded in the same form.

5. Breakthrough Memory limit of upload
Method One:
Modify the Memory_limit value in php.ini to a larger size, such as 64M
Method Two:
Dynamically modify the value of Memory_limit using the Apache rewrite method. First create a. htaccess file and save it in the current directory of the upload file program. The code is as follows:
Php_value Memory_limit 100M
Php_value post_max_size 30M
Php_value upload_max_filesize 30M
Php_value Max_execution_time 300
Php_value Max_input_tim 300
Php_value display_errors on
--eof--
Look at the above article, I hope you have something to gain.
Excerpt from the Red Hacker Alliance (www.7747.net): http://www.7747.net/kf/201009/73557.html

  • Related Article

    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.