Php file upload function move_uploaded_file

Source: Internet
Author: User
Tags error code php file upload
Upload files
It is necessary to allow users to upload files to many websites. Therefore, how can we achieve this? But then again, there are some convenient written documents. Have you considered the problem? Some of the help here is a little easier to digest.

First, you need to make a lie in the form of an HTML webpage. In this form, you must determine the maximum size of the file to be uploaded, and you must have the file upload field itself.

<Form enctype = "multipart/form-data" action = "upload. php" method = "POST">
<Input type = "hidden" name = "MAX_FILE_SIZE" value = "30000"/>
<Input name = "userfile" type = "file"/>
<Input type = "submit" value = "Send File"/>
</Form>

MAX_FILE_SIZE defines as many file sizes as possible, in bytes, of course. An important part of the name attribute Upload field (the type attribute of the specified file value, as we will use this later in our script to know which files will be uploaded (this is used to support multiple types of uploads ).
Next, we will upload the file. But first, it is important to understand the dollar _ FILE [] array.

In the dollar _ FILE [] array, there is a project that sends this script in the form of Upload in each field. Therefore, in our case, there is an index named 'userfield', for a field with this name in our form. The array of each value is also an associated array, the following items and values (rewrite explicitly from the file ):

<? Php
$ Target_file = & apos;/path/to/upload/& apos ;. basename ($ _ FILES [& apos; userfile & apos;] [& apos; name & apos;]);
?>
Key Value
The 'name' file name uses the client's computer.
MIME-type files of the 'type' type (such as image/gif format ). The browser does not always provide this, and the value should not be assumed to be correct.
Size of the file to be uploaded (in bytes ).
'Tmp_name 'file to the uploaded file, which should be transferred immediately and renamed after successful upload. Location is irrelevant, because the method you use knows the location of the file (move_uploaded_file, after I use it.
The 'error' error code (or successful code, if 0) occurs during upload. (Error code)

Table 1: Key and value

So now we can start the business. First, we get the file name, we will give it to the uploaded file, and the Directory, we want to put it.


<? Php
$ Target_file = & apos;/path/to/upload/& apos ;. basename ($ _ FILES [& apos; userfile & apos;] [& apos; name & apos;]);
?>

It is absolutely necessary to use basename. Without this, you are running a huge security risk. If you want to upload the file name, start to use the relative path to rise, such .. /.. /.. /, they will eventually get the root, and the path to the original starting point will be after more directories are implemented for the files that can be viewed anytime and anywhere.

Warning it is very important to ensure that the server has the permission to write to this directory. If your server is running any release version of Linux or Mac OS X, you may need the attribute directory.
Next, you only need to set the uploaded file to target_file. I think this is done for the sake of Test success.

<? Php
If (move_uploaded_file ($ _ FILES [& apos; userfile & apos;] [& apos; tmp_name & apos;], $ target_file )){
// Succeeded
Echo & apos; Yay! & Apos ;;
} Else {
// Failed
Echo & apos; No! & Apos ;;
}
?>


This is really the case. Very easy, huh? You can expand this point by reading the PHP file sequence and unserializing object. If you have any questions, you can post a forum at any time.

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.