Php file Upload move_uploaded_file

Source: Internet
Author: User

In a very useful aspect, PHP is capable of managing file uploads to your server. Allowing users to upload files to your server to open a whole worm, so please be careful when uploading advantageous files.


PHP-file upload: h tml form
Before you can use PHP to manage your uploads, you must first create an HTML form for the user to select the file to upload. See the lessons of our HTML form for more in-depth research.

<Form enctype = "multipart/form-data" action = "uploader. php" method = "POST">
<Input type = "hidden" name = "MAX_FILE_SIZE" value = "100000"/>
Choose a file to upload: <input name = "uploadedfile" type = "file"/> <br/>
<Input type = "submit" value = "Upload File"/>
</Form>

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

This is an important part of a brief description. The above code:

Is enctype = "multipart/form of data"-to work properly for the p hp file we will create.
Action = "uploader. php"-name our p hp page, will be created soon.
Method = "mail"-notify the browser that we want to send the information to this server for use.
Input type = "hidden" name = "horse...-maximum file size allowed by the set, in bytes, can be uploaded. This security mechanism is very easy to bypass and we will show a solid backup solution in p hp. In this example, we have set the maximum file size to KB.
Input name = "uploadedfile"-u ploadedfile is how we will access the file in our p hp script.
Save the code in this form to the file and call for upload.html. If you think it looks like the following in the browser:

Display:
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 process data and do all the work.

PHP-file upload: What is p hp?
Now, we have the right to use the PHP script in the HTML form where we can start code to process our uploads. In general, php files should make important decisions. All uploads: keep the files or discard them. Files may be thrown away for many reasons, including:

This file is too large and you don't want to put it on your server.
The person you want to upload images and other files, such as an executable file (. Exe ).
Problematic file upload, so you cannot keep it.
This example is very simple. If you ignore the code, this function will be added.

PHP-file upload: u ploader. php
When the uploader. Php file is executed, the uploaded file is stored on the server in a temporary storage area. If the file is not transferred to a different location, it will be destroyed! To save valuable FILES, we will use the variable $ _ FILES to join the array.

Use the $ _ FILES array to list all information FILES of PHP stores. There are two factors, so we need to understand this example. Www.111cn.net/phper/php.html

Uploadedfile-u ploadedfile is a reference in the h tml form we specified. We will need this notification to pass the $ _ FILES array file, we want to play.
Variable $ _ FILES ['uploadfile'] ['name']-name contains the user upload file in the original path.
Variable $ _ FILES ['uploadfile'] ['tmp_name ']-t mp_name contains the path. Temporary FILES are stored on the server. The file should be a temporary name of a temporary directory on the server.
Now, we can start to write a basic PHP script Upload manager! Here is how we can select a permanent name for the obtained temporary file name and select 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 ['uploadfile'] ['name']);

PHP-file upload: m ove_uploaded_file function
Now, all we have to do is call the move_uploaded_file function, so that PHP can do its best. Move_uploaded_file: 1) temporary file of the road (check! ) 2) path, it is transferred to (check! ).

$ Target_path = "uploads /";

$ Target_path = $ target_path. basename ($ _ FILES ['uploadfile'] ['name']);

If (move_uploaded_file ($ _ FILES ['uploadfile'] ['tmp _ name'], $ target_path )){
Echo "The file". basename ($ _ FILES ['uploadfile'] ['name']).
"Has been uploaded ";
} Else {
Echo "There was an error uploading the file, please try again! ";
}

For more information, see www.111cn.net/phper/php.html.

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.