Today we are going to continue to php9 a series of super global variables, today we'll talk about the use of $_file.
First of all, similar and $_post, $_files applicable to upload file class input tag, $_fifles similar to a two-dimensional array, you can get and save the client uploaded files of various information, specific as follows:
$_files[the original name of the ' MyFile ' [' name '] 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 stored in the server after the temporary file name, typically the system default. Can be specified in the php.ini upload_tmp_dir, but the putenv () function setting does not work.
$_files[' myFile ' [' Error '] and the file upload related error code. [' ERROR '] is added in PHP version 4.2.0. Here's what it says: (They're constant after PHP3.0)
Therefore, through the above variable can be used for background processing user uploaded files, such as only upload picture class, you can through the $_files[' myFile ' [' type '] to limit, limit the size can be through the $_files[' myFile ' [' Size '] To specify, next, I use the example on W3school to explain the specific usage of $_files.
The main function of the 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:
Filename:
The code is simple, just defines an upload control, noteThis name corresponds to the value ' file ' is the basis for $_files to obtain the foreground file, if the background to obtain file types written $_files[' myFile ' [' type '], indicating the name of the front desk must be ' myFile ', otherwise will be reported invalid File's error. Finally, look at the background code:
-->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 explode (), end (), and In_array () three main functions, its usage I have done a comment explanation, are very good understanding, you see, using PHP to upload a file is how simple, need to note that: After the file is uploaded, By default, it is stored in a temporary directory that can be accessed by $_files[' myFile ' [' Tmp_name '], which you must remove or move from the temp directory to other places, and if not, it will be deleted. That is, whether or not the upload succeeds, the files in the temp directory will be deleted after the script is executed. So this example uses the Move_uploaded_files () function to copy it to another location, at which point the upload file process is completed.
Finally, if you need to download the source code, although very simple.