Introduction to the usage of $_FILES global variable for php file upload_PHP tutorial

WBOY
Release: 2016-07-20 11:03:06
Original
1126 people have browsed it

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.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445307.htmlTechArticleThis article will briefly introduce the important global variable $_FILES when uploading files and images in PHP. Parameters, friends who need to know can refer to it. File upload...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template