PHP learning notes-File Upload ($ _ files, is_uploaded_file (), move_uploaded_file ())

Source: Internet
Author: User

Knowledge points used:
① $ _ FILES Global Array
② Bool is_uploaded_file (string $ filename)
③ Bool move_uploaded_file (string $ filename, string $ destination)

PHPCode:

<? PHP
Echo "<PRE> ";
Print_r ( $ _ Files );
Echo "<PRE> ";
If ( $ _ Files ['Myfile'] ['type'] = "application/octet-stream ") //
{
Echo "You uploaded an application. Program File <br> ";
Echo "File name :". $ _ Files ['Myfile'] ['name'];
}

$ File_path = "C :\\ Windows \ temp \\". $ _ Files ['Myfile'] ['name'];


If ( Is_uploaded_file ($ _ Files ['Myfile'] ['tmp _ name']) {
Echo "<Br> This file was uploaded ...";
$ Move_from = $ _ Files [Myfile] [tmp_name];
$ Move_to = $ _ Server ['Document _ root']. "/upload /". $ _ Files ['Myfile'] ['name'];
If ( Move_uploaded_file ( $ Move_from ,Iconv "UTF-8", "GBK" $ Move_to) )){
Echo "File Uploaded successfully! ";
Echo "Path :". $ Move_to ;
}

} Else {
Echo "This file is local ...";
}
?>
<Form action = "Upload. php" method = "Post" enctype = "multipart/form-Data">
Username: <input name = "name" type = "text">
Password: <input name = "password" type = "password">
<Input type = "file" name = "myfile"/>
<Input type = "Submit" value = "Upload"/>
</Form>

① Using the Global Array $ _ files of PHP, you can upload files from the client's computer to a remote server.
The first parameter is the input name of the form, and the second subscript can be "name", "type", "size", "tmp_name" or "error ". Like this:

$ _ FILES ["myfile"] ["name"]-name of the uploaded file
$ _ FILES ["myfile"] ["type "]- type of the file to be uploaded
$ _ FILES ["myfile"] ["size"]-size of the file to be uploaded, in bytes
$ _ FILES ["myfile"] ["tmp_name"]-name of the temporary copy of the file stored on the server
$ _ FILES ["myfile"] ["error"]-error code caused by File Upload
print_r ($ _ files) output result:
array
(
[myfile] => array
(
[name] => Use Description document .txt
[type] => text/ plain
[tmp_name] => D: \ Wamp \ TMP \ php56c5. TMP
[Error] => 0
[size] => 1278
)

② The is_uploaded_file () function checks whether the specified file was uploaded through http post,
True is returned, or false is returned. This can be used to prevent malicious users from deceiving scripts to access files that cannot be accessed, such as/etc/passwd.

③ The move_uploaded_file () function checks whether the file specified by $ filename is a valid Upload File (that is, the File Uploaded through the http post upload mechanism ),
Yes, true is returned, and the specified file is moved to the file specified by $ destination. Otherwise, false is returned.
This check is especially important. If the uploaded file may display the content to the user or other users in the system.
Note: If the target file already exists, it will be overwritten.

Supplement:

By default, our PHP file's default encoding is UTF-8,

But our file system only knows gb2312 or GBK, so according to the above
The File Uploaded By code may contain garbled characters in the folder. To solve this problem, you can use the iconv () function to convert the encoding format.
Function Description:
String iconv (string $ in_charset, string $ out_charset, string $ Str)
In_charset: Your original file encoding
Out_charset: encoding of the file to be converted

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.