PHP file upload code Description

Source: Internet
Author: User
Tags php file upload

Copy codeThe Code is as follows:
<? Php
If ($ _ FILES ['myfile'] ['name']! = ''){
If ($ _ FILES ['myfile'] ['error']> 0 ){
Echo "error status:". $ _ FILES ['myfile'] ['error'];
} Else {
Move_uploaded_file ($ _ FILES ['myfile'] ['tmp _ name'], "uploads/". $ FILES ['myfile'] ['name']);
Echo "<script> alert (uploaded successfully !); </Script> ";
}
} Else {
Echo "<script> alert (please upload a file !); </Script> ";
}
?>

Note:

Before explaining this code, we need to understand the following knowledge.

$ _ FILES ['myfile'] ['name'] indicates the name of the uploaded file.
$ _ FILES ['myfile'] ['type'] indicates the type of the file to be uploaded.
$ _ FILES ['myfile'] ['SIZE'] indicates the size of the uploaded file, in bytes (B)
$ _ FILES ['myfile'] ['tmp _ name'] indicates the name of the temporary copy file in the server where the uploaded file exists, after the file is moved to the specified directory, the temporary file will be automatically destroyed.
$ _ FILES ['myfile'] ["error"] indicates the status code of an error that may occur during file upload. the status code will be described later.

After learning about this, let's take a look at the upload. php code.

First, myfile in $ _ FILES ['myfile'] ['name'] indicates the name value of the file tag uploaded on the preceding HTML page, based on this, we can know which input file we are processing is submitted.

Then, let's determine whether $ _ FILES ['myfile'] ['name'] is null. Based on this, we can know whether the user has uploaded the file and then perform different operations.

If a file is uploaded and its status is 0, the upload is successful. We can use the move_uploaded_file method to store the uploaded file to the specified directory, the above example refers to moving the uploaded file to the uploads folder under the same directory. This path is relative to this PHP file (both upload. php.

For example, we want to move the uploaded file to upload. in the above php folder named user, we can write: move_uploaded_file ($ _ FILES ['myfile'] ['tmp _ name'], ". /user /". $ FILES ['myfile'] ['name']). This method is convenient and flexible, so that a file is uploaded to the server, you can open a directory on the server to view the file.

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.