Processing of file upload forms
The code is as follows: |
Copy code |
<Form action = "post. php" enctype = "multipart/form-data" method = "post"> <Input name = "MAX_FILE_SIZE" type = "hidden" value = "1000"/> <Input name = "upfile" type = "file"/> <Input type = "submit" value = "Upload file"/> </Form> |
Let's take a look at the usage of files variables.
The $ _ FILES Super global variable is special. It is the unique two-dimensional array in the predefined Super Global array. Its function is to store various information related to the file to be uploaded, which is crucial for files uploaded to the server through PHP scripts. There are a total of five items in this function:
1. $ _ FILES ["userfile"] ["error"]
$ _ FILES ["userfile"] ["error"] the array value provides important information related to the upload attempt results. There are 5 different return values in total, one of which indicates successful results, and the other four indicate special errors during the attempt. The names and descriptions of the return values are described later.
2. $ _ FILES ["userfile"] ["name"]
$ _ FILES ["userfile"] ["name"] variable specifies the initial name of the file declared on the client machine, including the extension. For this reason, if a file named vacation.jpg is uploaded through the table list, the value of this change will be vacation.png.
3. $ _ FILES ["userfile"] ["size"]
$ _ FILES ["userfile"] ["size"] variable specifies the size of the file uploaded from the client, in bytes. For this reason, in the vacation.jpg File example, this function may be assigned a value of 5253, about 5kb.
4. $ _ FILES ["userfile"] ["tmp_name"]
$ _ FILES ["userfile"] ["tmp_name"] variable specifies the temporary name assigned to the file after being uploaded to the server. This is the name of the file stored in the temporary directory (specified by the PHP command upload_tmp_dir.
5. $ _ FILES ["userfile"] ["type"]
$ _ FILES ["userfile"] ["type"] variable specifies the mime type of the file uploaded from the client. For this reason, in the vacation.jpg File example, this variable is assigned an image/jpeg value. If a PDF file is uploaded, the value is application/pdf. Because this variable sometimes gets unexpected results, it should be displayed in the script for verification.
$ _ FILES ['myfile'] ['error'] error code related to the file upload. ['Error'] is added in PHP 4.2.0. The following is its description: (they become constants after PHP3.0)
UPLOAD_ERR_ OK
Value: 0. If no error occurs, the file is 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 is uploaded.
UPLOAD_ERR_NO_FILE
Value: 4; no file is uploaded.
Value: 5; the size of the uploaded file is 0.
Now let's look at a complete instance.
The code is as follows: |
Copy code |
<Html> <Body> <Form action = "upload_file.php" method = "post" Enctype = "multipart/form-data"> <Label for = "file"> filename: </label> <Input type = "file" name = "file" id = "file"/> <Br/> <Input type = "submit" name = "submit" value = "submit"/> </Form> </Body> </Html> |
Php code
<? Php
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"]. "<br/> ";
}
Else
{
Echo "upload:". $ _ files ["file"] ["name"]. "<br/> ";
Echo "type:". $ _ files ["file"] ["type"]. "<br/> ";
Echo "size:". ($ _ files ["file"] ["size"]/1024). "kb <br/> ";
Echo "stored in:". $ _ files ["file"] ["tmp_name"];
}
}
Else
{
Echo "invalid file ";
}
?>
For IE, the jpg file type must be pjpeg, and for FireFox, it must be jpeg.