PHP9 the usage of a Super global variable (iii) _php tutorial

Source: Internet
Author: User
Tags http post
Today to continue to php9 a series of super-global variables, today to talk about the use of $_file.

First of all, similar to $_post and so on, $_files applies to the upload file class input tag, $_fifles similar to a two-dimensional array, you can get and save the client uploaded files of various information, as follows:

$_files[' myFile ' [' Name '] The original name of the client file.
The MIME type of the $_files[' myFile ' [' type '] file requires the browser to provide support for that information, such as "Image/gif".
$_files[' myFile ' [' size '] the size of the uploaded file, in bytes.
$_files[' myFile ' [' tmp_name '] files are uploaded after the temporary file name stored on the server, usually the system default. Can be specified in the php.ini upload_tmp_dir, but the putenv () function setting is not working.
$_files[' myFile ' [' Error '] and the file upload related error code. [' ERROR '] was added in version PHP 4.2.0. Here is a description of it: (they become constants after PHP3.0)

Therefore, through the above write variable can be used in the background processing user uploaded files, such as only upload the picture class, you can use $_files[' myFile ' [' type '] to limit, limit size can be through $_files[' myFile ' [' Size '] To specify, next, I use the example on W3school to explain the specific use of $_files.

The main function of this demo is to upload the file of the picture class and save it in the relevant directory on the server side.

The HTML code is as follows:



The code is simple, just define an upload control, noteThis name corresponds to the value of ' file ' is the basis for $_files to obtain the foreground file, if the background to obtain the file type written $_files[' myFile ' [' type '], indicating the foreground name corresponding value must be ' myFile ', otherwise it will be reported invalid The file error. Finally, look at the code behind the scenes:

 0) {echo "Return Code:". $_files["File" ["Error"]. "
";} Else{echo "Upload:". $_files["File" ["Name"]. "
"; echo" Type: ". $_files["File" ["type"]. "
"; echo" Size: ". ($_files["File" ["Size"]/1024). The 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";}? >


The main use of the explode (), end (), and In_array () three main functions, the use of which I have done a comment explanation, are very good understanding, you see, with PHP implementation of uploading a file is how simple, it is important to note that the file is uploaded after the end, It is stored in the temp directory by default, and this directory can be accessed by $_files[' myFile ' [' Tmp_name '], which you must remove from the temporary directory or move to somewhere else, if not, it will be deleted. That is, regardless of whether or not the upload succeeds, the files in the temp directory will be deleted after the script executes. So in this case, the Move_uploaded_files () function is used to copy it to a different location, and this is the process of uploading the file.

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

http://www.bkjia.com/PHPjc/664285.html www.bkjia.com true http://www.bkjia.com/PHPjc/664285.html techarticle today to continue to php9 a series of super-global variables, today talk about the use of $_file. First of all, classes and $_post, etc., $_files for uploading the file class input tag, $_fifles class in a two ...

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