Today I will continue the series of php9 super global variables. Today I will talk about the usage of $_FILE.
First of all, similar to $_POST, etc., $_FILES is suitable for input tags of uploaded files. $_FIFLES is similar to a two-dimensional array, which can obtain and save various information about files uploaded by the client, as follows:
$_FILES['myFile']['name'] The original name of the client file.
$_FILES['myFile']['type'] The MIME type of the file, which requires the browser to provide support for this information, such as "image/gif".
$_FILES['myFile']['size'] The size of the uploaded file, in bytes.
$_FILES['myFile']['tmp_name'] The temporary file name stored on the server after the file is uploaded, usually the system default. It can be specified in upload_tmp_dir in php.ini, but setting it with the putenv() function has no effect.
$_FILES['myFile']['error'] The error code related to the file upload. ['error'] was added in PHP 4.2.0. The following is its description: (They become constants after PHP3.0)
Therefore, the above variables can be used to process files uploaded by users in the background. For example, if you can only upload pictures, you can limit them through $_FILES['myFile']['type'], and limit the size. Specify it through $_FILES['myFile']['size']. Next, I will use an example on w3school to explain the specific usage of $_FILES.
The main function of this demo is to upload image files and save them in the relevant directory on the server side.
The html code is as follows:
<html> <body> </body> </html>
The code is very simple. It just defines an upload control. Note that the value 'file' corresponding to this name is the basis for $_FILES to obtain the front-end file. If the file type is obtained in the background, write $_FILES['myFile']['type'] , indicating that the value corresponding to the name at the front desk must be 'myFile', otherwise an Invalid file error will be reported. Finally, take a look at the background code:
<?php $allowedExts = array("gif", "jpeg", "jpg", "png"); /* * explode(separator,string,limit) 参数 描述 separator 必需。规定在哪里分割字符串。 string 必需。要分割的字符串。 limit 可选。规定所返回的数组元素的最大数目。 */ $temp = explode(".", $_FILES["file"]["name"]); //end() 函数将数组内部指针指向最后一个元素,并返回该元素的值(如果成功)。 $extension = end($temp); if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 200000) /* * in_array(value,array,type) * in_array() 函数在数组中搜索给定的值。 * 如果给定的值 value 存在于数组 array 中则返回 true。如果第三个参数设置为 true,函数只有在元素存在于数组中且数据类型与给定值相同时才返回 true。 如果没有在数组中找到参数,函数返回 false。 注释:如果 value 参数是字符串,且 type 参数设置为 true,则搜索区分大小写。 * * move_uploaded_file() 函数将上传的文件移动到新位置。 * move_uploaded_file(file,newloc) * 本函数检查并确保由 file 指定的文件是合法的上传文件(即通过 PHP 的 HTTP POST 上传机制所上传的)。如果文件合法,则将其移动为由 newloc 指定的文件。*/ && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; 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"; } ?>
Finally, if necessary, you can still download the source code, although it is very simple.