How PHP uploads files to a specified location (with code)

Source: Internet
Author: User
This article brings you the content is about how PHP upload files to the specified location (with code), there is a certain reference value, the need for friends can refer to, I hope to help you.

test.php (front-end code)

<! DOCTYPE html>

Explain:

    • The method under the From label must be a post

    • To add enctype= "Multipart/from-data"

do.php (from form processing file)

<?phpprint_r ($_files);?>

Explain:

Print_r () Outputs the data passed by the table only son as an array.

The output is:

Array (     [img] = = Array (         [name] + 1.jpg          [Type] = Image/jpeg         [Tmp_name] = C:\Windows\Temp\ Phpff07.tmp         [ERROR] = 0         [size] = 17164     ))
    • Name: Names of uploaded files

    • Type: The MIME type of the uploaded file

    • Tmp_name: Temporary file name uploaded to server (or local)

    • Size: Sizes of uploaded files

    • Error: The wrong number of the upload file

do.php (Successful implementation of file upload to the specified folder)

Method One:

<?phpprint_r ($_files), $filename =$_files[' img ' [' name ']; $type =$_files[' img ' [' type ']; $tmp _name=$_files[' img ' [' Tmp_name ']; $size =$_files[' img ' [' size ']; $error =$_files[' img '] [' ERROR '];//move temporary files on the server to the specified directory Move_uploaded_ File ($tmp _name, "img/". $filename);

Where $_files is a file upload variable, storing the data uploaded by the file

Move_uploaded_file ($tmp _name, specify folder name. $filename);

Moves temporary files on the server to the specified directory, returns true successfully, fails to return false

Method Two:

Copy ($tmp _name, "img/". $filename);
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.