Usage of php9 super global variables (III)

Source: Internet
Author: User

Today, we will continue with the php9 series of super global variables. Today we will talk about $ _ FILE usage.

First of all, like $ _ POST, $ _ FILES applies to the input tag of the file upload class. $ _ records les is similar to a two-dimensional array and can obtain and save various information about the FILES uploaded by the client, the details are as follows:

$ _ FILES ['myfile'] ['name'] original name of the client file.
$ _ FILES ['myfile'] ['type'] indicates the MIME type of the file, which must be supported by the browser, for example, "image/gif ".
$ _ FILES ['myfile'] ['SIZE'] size of the uploaded file, in bytes.
$ _ FILES ['myfile'] ['tmp _ name'] temporary file name stored on the server after the file is uploaded, which is generally the default file name. It can be specified in upload_tmp_dir of php. ini, but the putenv () function setting does not work.
$ _ FILES ['myfile'] ['error'] error Code related to the file upload. ['Error'] is added in PHP 4.2.0. The following is its description: (they become constants after PHP3.0)

Therefore, the preceding variables can be used in the background to process user-uploaded files. For example, only image files can be uploaded, you can use $ _ FILES ['myfile'] ['type'] to limit the size. You can use $ _ FILES ['myfile'] ['SIZE'] to specify the size, next, I will explain the usage of $ _ FILES using the w3school example.

This demo mainly implements the function of uploading image files and saving them to related directories on the server.

The html code is as follows:

 


The code is very simple. It just defines an upload control. Note:The value 'file' corresponding to this name is $ _ FILES to obtain the basis for foreground FILES. If the background obtains the file type, write $ _ FILES ['myfile'] ['type'], it indicates that the value corresponding to the frontend name must be 'myfile', otherwise the Invalid file error will be reported. Finally, let's take a look at the background code:

 0) {echo "Return Code:". $ _ FILES ["file"] ["error"]."
";} Else {echo" Upload: ". $ _ FILES [" file "] [" name "]."
"; Echo" Type: ". $ _ FILES [" file "] [" type "]."
& Quot; echo & quot; Size: & quot;. ($ _ FILES [& quot; file & quot;] [& quot; size & quot;]/1024). & quot; kB
"; Echo" Temp file: ". $ _ FILES [" file "] [" tmp_name "]."
"; If (file_exists (" upload /". $ _ FILES ["file"] ["name"]) {echo $ _ FILES ["file"] ["name"]. "already exists. ";} else {move_uploaded_file ($ _ FILES [" file "] [" tmp_name "]," upload /". $ _ FILES ["file"] ["name"]); echo "Stored in :". "upload /". $ _ FILES ["file"] ["name"] ;}} else {echo "Invalid file" ;}?>


It mainly uses three main functions: explode (), end (), and in_array (). I have made comments and explanations on their usage. They are all very easy to understand. You can see, how simple it is to upload a file using php. Note that after the file is uploaded, it is stored in a temporary directory by default, this directory can be accessed through $ _ FILES ['myfile'] ['tmp _ name']. In this case, you must delete it from the temporary directory or move it to another place. If no, will be deleted. That is, no matter whether the upload is successful or not, the files in the temporary directory will be deleted after the script is executed. In this example, use the move_uploaded_files () function to copy the file to another location.

Finally, if necessary, you can download the source code, although very simple.

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.