How PHP handles forms to upload files

Source: Internet
Author: User
When using the form to upload a file, the form must be submitted by post, the input control of the upload file type property value should be file, most importantly, the form label needs to add a enctype= "Multipart/form-data" property. This article is mainly to share with you the PHP processing form upload file method, hope to help everyone.

HTML code:

    1. <! DOCTYPE html>  

PHP Code:

  1. <?php/* * Use $_files to receive file data uploaded through the form *$_files[' File1 '] in file1 corresponding form in the input tag of the upload file in the name Value *$_files[' File1 ']      An array is returned: $_files[' file1 ' [' name '] Displays the original name of the uploaded file.      $_files[the MIME type of the ' file1 ' [' type '] file, such as "Image/gif", "Image/png".      $_files[' file1 ' [' size '] the size of the uploaded file, in bytes.      $_files[' file1 ' [' tmp_name '] stores temporary file names and temporary stored paths.      $_files[' file1 ' [' Error '] and the file upload related error code. = 0;       No error occurred and the file upload was successful. = 1;       The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini. = 2;       The size of the uploaded file exceeds the value specified by the Max_file_size option in the HTML form. = 3;       Only part of the file is uploaded. = 4;       No files were uploaded. = 5;       The upload file size is 0.        */$file =$_files[' file1 '];        Rename the file, try not to start the Chinese name, as follows timestamp + random number rename $filename =time (). rand (0,1000); Gets the suffix of the file, PathInfo () returns the path information of a file as an array, where the extension element is the suffix name of the file $ext =pathinfo ($_files[' file1 ' [' name ']) ['        Extension ']; The final file name is $filename = $filename. '. '.        $ext; Setting the location where the file is uploaded to the server, Move_uploaded_file () moves the file to the new location, the first parameter is the file to move, and the second parameter is where to move.      I am here to put in the same directory as this PHP file, note the need to connect the new file name. if (mOve_uploaded_file ($file [' tmp_name '], './'. $filename)} {echo "Success";       } else{echo "error"; }?>

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.