As we all know, file upload is an essential part of some website applications. For example, a personal blog uploads personal portraits and some learning materials shared by forums. This involves the knowledge of using forms to process file uploads, in PHP, we can use the Global Array $ _ files for processing. Next let's take a look at the basic file upload Processing Section. First of all, being able to upload files is a security risk. Therefore, when developing this function, you must pay attention to the security of your website. This article only deals with the basic part of file upload, with little consideration for security, and hopes to learn from advanced applications in the future.
In the PHP predefined variables section, we have made some rough learning about some predefined variables, including the global variable $ _ files, now I want to learn more about the global volume, because I need to use its functions when processing file uploads.
$ _ FILES parameters:
$ _ FILES ["file"] ["name"]-name of the uploaded file
$ _ FILES ["file"] ["type"]-type of the file to be uploaded
$ _ FILES ["file"] ["size"]-size of the uploaded file, in bytes
$ _ FILES ["file"] ["tmp_name"]-name of the temporary copy of the file stored on the server
$ _ FILES ["file"] ["error"]-errors caused by File UploadCode
$ _ FILES ["file"] ["error"] ["error"] value:
Upload_err_ OK
0: no error occurred. The file is uploaded successfully.
Upload_err_ini_size
1: the uploaded file exceeds the limit of the upload_max_filesize (2 MB by default) option in PHP. ini.
Upload_err_form_size
2: the size of the uploaded file exceeds the value specified by the max_file_size option in the HTML form.
Upload_err_partial
3: only part of the file is uploaded.
Upload_err_no_file
4: no files are uploaded.
5: The file size is 0.
Next let's take a look at the most basic file upload:
<HTML>
<Body>
<Form action = "upload-file.php" method = "post"
Enctype = "multipart/form-data">
<Label for = "file"> file name: </label>
<Input type = "file" name = "file" id = "file"/>
<Br/>
<Input type = "submit" name = "submit" value = "submit"/>
</Form>
</Body>
</Html>
This html page is shown in the following figure:
The upload-file.php code is as follows:
<? PHP
If ($ _ FILES ["file"] ["error"]> 0)
{
Echo "error:". $ _ FILES ["file"] ["error"]. "<br/> ";
}
Else
{
Echo "file name:". $ _ FILES ["file"] ["name"]. "<br/> ";
Echo "type:". $ _ FILES ["file"] ["type"]. "<br/> ";
Echo "Size:". ($ _ FILES ["file"] ["size"]/1024). "kb <br/> ";
Echo "storage location:". $ _ FILES ["file"] ["tmp_name"];
}
?>
We can upload a Word file to see how the processing result is:
File Name: css.doc
Type: Application/MSWord
The size is 81.5 kb.
Storage location: C: \ WINDOWS \ temp \ php7d. tmp
As you can see, this file is saved to the C: \ WINDOWS \ temp \ temporary directory. You may want to see if this file exists in this directory, but the result is: No! Why? Since PHP deleted the file generated after executing this script, it needs to be further processed after the upload. In addition, in windows, temporary files generated by PHP, such as php7d. tmp, are regular. That is to say, the file uploaded in the next form should be like this: php7e. tmp
To save the uploaded file, use the move_uploaded_file function. Create an upload folder in the same directory of your upload-file.php file. Next let's take a look at the example above:
<? PHP
If ($ _ FILES ["file"] ["error"]> 0)
{
Echo "error:". $ _ FILES ["file"] ["error"]. "<br/> ";
}
Else
{
Echo "file name:". $ _ FILES ["file"] ["name"]. "<br/> ";
Echo "type:". $ _ FILES ["file"] ["type"]. "<br/> ";
Echo "Size:". ($ _ FILES ["file"] ["size"]/1024). "kb <br/> ";
}
If (file_exists ("upload/". $ _ FILES ["file"] ["name"])
{
Echo $ _ FILES ["file"] ["name"]. "The file already exists.";
}
Else
{
Move_uploaded_file ($ _ FILES ["file"] ["tmp_name"],
"Upload/". $ _ FILES ["file"] ["name"]);
Echo "the file has been stored to:". "upload/". $ _ FILES ["file"] ["name"];
}
?>
Let's take a look at the results:
File Name: css.doc
Type: Application/MSWord
The size is 81.5 kb.
The file has been stored in: Upload/css.doc
After this step, we can obtain the uploaded file at the specified place (upload. At this point, the most basic file upload is complete. We also need to consider the file type and size when uploading files. Let's discuss these aspects later.