This article will briefly introduce the various parameters of $_FILES, an important global variable when uploading files and images in PHP. Friends who need to know more can refer to it.
Processing of file upload form
代码如下 | 复制代码 |
Let’s take a look at the usage of variables in files
The $_FILES super global variable is very special. It is the only two-dimensional array among the predefined super global arrays. Its role is to store various information related to uploaded files, which is crucial for files uploaded to the server through PHP scripts. There are a total of 5 items in this function:
1.$_FILES["userfile"]["error"]
The $_FILES["userfile"]["error"] array value provides important information about the results of the upload attempt. There are 5 different return values in total, one indicating a successful result and the other 4 indicating specific errors that occurred during the attempt. The names and names of the return values will be introduced later.
2.$_FILES["userfile"]["name"]
The $_FILES["userfile"]["name"] variable specifies the initial name of the file declared on the client machine, including the extension. So if you browser a file called vacation.jpg and upload it via a form, the value of this variable will be vacation.png.
3.$_FILES["userfile"]["size"]
The $_FILES["userfile"]["size"] variable specifies the size of the file uploaded from the client, in bytes. Therefore, in the example of vacation.jpg file, this function may be assigned a value of 5253, which is approximately 5kb.
4. $_FILES["userfile"]["tmp_name"]
The $_FILES["userfile"]["tmp_name"] variable specifies the temporary name given to the file after uploading to the server. This is the filename specified when stored in the temporary directory (specified by the PHP directive upload_tmp_dir).
5. $_FILES["userfile"]["type"]
The $_FILES["userfile"]["type"] variable specifies the mime type of the file uploaded from the client. Therefore, in the example of the vacation.jpg file, this variable will be assigned the value image/jpeg. If the upload is a PDF, the value is application/pdf. Because this variable can sometimes give unexpected results, it should be verified explicitly in the script.
$_FILES['myFile']['error'] 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)
UPLOAD_ERR_OK
Value: 0; No errors occurred and the file was uploaded successfully.
UPLOAD_ERR_INI_SIZE
Value: 1; The uploaded file exceeds the limit of the upload_max_filesize option in php.ini.
UPLOAD_ERR_FORM_SIZE
Value: 2; The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
UPLOAD_ERR_PARTIAL
Value: 3; Only part of the file was uploaded.
UPLOAD_ERR_NO_FILE
Value: 4; No files were uploaded.
Value: 5; Upload file size is 0.
Okay, let’s take a look at a complete example
The code is as follows | Copy code | ||||
|
php code
if ((($_files["file"]["type"] == "image/gif")
|| ($_files["file"]["type"] == "image/jpeg")
|| ($_files["file"]["type"] == "image/pjpeg"))
&& ($_files["file"]["size"] < 20000))
{
if ($_files["file"]["error"] > 0)
{
echo "error: " . $_files["file"]["error"] . "
";
}
else
{
echo "upload: " . $_files["file"]["name"] . "
";
echo "type: " . $_files["file"]["type"] . "
";
echo "size: " . ($_files["file"]["size"] / 1024) . " kb
";
echo "stored in: " . $_files["file"]["tmp_name"];
}
}
else
{
echo "invalid file";
}
?>
For IE, the type to recognize jpg files must be pjpeg, and for FireFox, it must be jpeg.