PHP File Upload

Source: Internet
Author: User
Tags php file upload

On the server, Judge:

1) receive data post 2)enctype= "Multipart/form-data" Property

3) Move_uploaded_file: Move the file to the specified location

PHP.ini Temporary file configuration File_uploads = on upload_tmp_dir = "

Vanishing time for temporary files: temporary files disappear immediately after the. php file is accepted.

If not, the contents of the temporary file can be observed when the PHP file is not finished;

The client determines the size of the upload file: JS is not allowed to manipulate local files

Ajax is unable to determine the size of the uploaded file at the client

1.php File Upload process:

To do a file upload form:

<form action= "up.php" method= "POST" >
User name: <input type= "text" name= "username"/><br/>
Avatar: <input type= "file" name= "Pic"/><br/>
<input type= "Submit" value= "Submission"/>
</form>

1) After submission, the file is automatically sent to the server, forming a temporary file,

2) on the server, just need to move the temporary files to their desired location, you can complete the upload file

After PHP forms a temporary file, it also forms a $_files super global array

The array holds the temporary address, temporary name, size, and other information of the file.

Note: File upload must be submitted with post

In the up.php:

<?php
Print_r ($_files);
?>

An empty array is printed on the browser at this time

WORKAROUND: Declare the ENCTYPE option

That is: Declare in form: Enctype= "Multipart/form-data" property

Notifies the other server of the binary information form data that contains the file

It will appear:

{

Name: Upload file name

Type: File type

Tem_name: Temporary file name

Error: Fault code

Size: The file size is expressed in bytes;

}

3) How to move the uploaded temporary files

Using the Move_uploaded_file () function <?php

Print_r ($_files);

if (Move_uploaded_file ($_files[' pic '] [' tmp_name '], './file/'. $_files[' pic ' [' name '])) {
echo "Success";
}else{
echo "fail";

}

PHP File Upload

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.