PHP File Upload move_uploaded_file_php Tutorial

Source: Internet
Author: User
Tags php file upload
This article is about PHP file upload, Move_uploaded_file Here we will also use Is_uploaded_file to determine whether the file is uploaded successfully. OK, let's start the tutorial.

This article is about php file upload ,move_uploaded_file Here we will also use Is_uploaded_file to determine whether the file is uploaded successfully. OK, let's start the tutorial.

A very useful aspect of PHP is the ability to manage files uploaded to your server. Allow users to upload files to your server to open up a whole worm, so please be careful with favorable file uploads.


PHP-File Upload: H tml form
When you can use PHP to manage your uploads, you must first create an HTML form that lets the user select the file to upload. See the lessons of our HTML form for a more in-depth study form.

Www.111cn.cn/phper/php.html

This is a short description of the important part of the above code:

is enctype = "multipart/form of data"-to work properly for the P hp file that we will be building.
Action = "uploader.php"-the name of our P HP page that will be created shortly.
Method = "POST"-notifies the browser that we want to send the information to the server after use.
input type = "hidden" name = "Horse ...-the maximum file size allowed for the set, in bytes, can be uploaded." This security mechanism is very easy to bypass and we will show a solid backup solution at p HP. We have set a file size limit of 100KB so far in this example.
Enter the name = "UploadedFile"-u ploadedfile is how we will access the file in our P hp script.
Save this form of code to the file and call it upload.html. If you think it's in the browser it should look like this:

Show:
Select the file to upload:
After the user clicks Submit, the data will be posted to the server and the user will be redirected to uploader.php. This PHP file will form the data of the process and do all the work.

PHP-File Upload: What is P hp doing?
Now we have the right HTML form in which we can start the code for the PHP script that is going to process our uploads. Typically, PHP files should make important decisions, all uploads: Keep files or throw them away. The file may be thrown away from many reasons, including:

The file is too large and you do not want to put it on your server.
You want people to upload pictures and upload something else, like an executable file (. EXE).
There is a problem with the file upload, so you cannot keep it.
This example is very simple, ignoring the code, will add this functionality.

PHP-File Upload: U ploader.php
When the uploader.php file is executed, the uploaded file exists on the server in a temporary storage area. If the file is not transferred to a different location it will be destroyed! In order to save valuable files We are going to use the variable $ _files associative array.

Through the $ _files array php where all information about the store is archived. There are two factors to this, and we need to understand this example. Www.111cn.cn/phper/php.html

Uploadedfile-u Ploadedfile is a reference to our designated H tml form. We will need this notification via $ _files array of files that we want to play.
Variable $ _files [' uploadedfile '] [' name ']-the name of the user that contains the original path to upload the file.
The variable $ _files [' uploadedfile '] [' tmp_name ']-t mp_name contains the path to which the temporary file is stored on the server. The file should exist in the temporary name of a temporary directory on the server.
Now, we can finally start writing a basic PHP script upload Manager! Here's how we'll get the temporary file name, choose a permanent name, and choose a place to store the file.

Where the file is going to be placed
$target _path = "uploads/";

/* ADD the original filename to our target path.
Result is "uploads/filename.extension" */
$target _path = $target _path. basename ($_files[' uploadedfile ' [' name ']);

PHP-File Upload: M ove_uploaded_file function
Now, all we have to do is invoke the Move_uploaded_file feature and let PHP do its own magic. Move_uploaded_file function needs to know 1) Road temporary files (check!) ) 2) path, it is transferred to (check!) ) 。

$target _path = "uploads/";

$target _path = $target _path. basename ($_files[' uploadedfile ' [' name ']);

if (Move_uploaded_file ($_files[' uploadedfile ' [' tmp_name '], $target _path)) {
echo "The file". basename ($_files[' uploadedfile ' [' Name ']).
"has been uploaded";
} else{
echo "There was a error uploading the file, please try again!";
}

Reprint Please specify: www.111cn.cn/phper/php.html


http://www.bkjia.com/PHPjc/445499.html www.bkjia.com true http://www.bkjia.com/PHPjc/445499.html techarticle This article is about PHP file upload, Move_uploaded_file Here we will also use the Is_uploaded_file to determine whether the file is uploaded successfully. Well, let's start with the tutorial. This article is about ...

  • 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.