Interpreting the handling of uploaded files in PHP,
When we edit our own information in the browser, we will see an upload avatar. In the library, we will upload the document... the word "Upload" is everywhere.
Php is the best language (programmers in other languages should not beat me ...). Php has a natural advantage in processing interaction, and naturally has powerful functions to process uploaded files.
Similar to submitting common data, uploading files also requires a form. Create a special form to upload files.
<Form enctype = "multipart/form-data" action = "upload_file.php" name = "upload_form" method = "post"> <! -- MAX_FILE_SIZE must be before all input. If you want to use an upload form in the future, you can write the hidden input --> <input type = "hidden" name = "MAX_FILE_SIZE" value = ""/> file after form: <input type = "file" name = "userfile"/> <input type = "submit" name = "sub_button" value = "Upload file submit button"/> </form>
OK. Let's analyze the code snippet.
The preceding enctype specifies the encoding format used when data is sent to the server. It has three values:
MAX_FILE_SIZE the hidden field (in bytes) must be placed before the input field of the file, and its value is the maximum size of the file. This is a suggestion for the browser. php will also check this item. However, this obstacle can be bypassed on the browser side, so do not expect it to block large files. However, the maximum file size is limited by post_max_size = (number) M in php. ini. However, we recommend that you add this project to avoid the trouble of uploading large files only after you have spent time uploading large files.
After the user submits the file form, the server can accept the data. In PHP, the global variable $ _ FILES is used to process FILES. Assume that the uploaded field name is userfile (which can be changed randomly in the field ).
$ _ FILES ['userfile'] ['name'] original name of the client file.
$ _ FILES ['userfile'] ['type'] indicates the MIME type of the file. This parameter is not checked on the PHP end, so this value does not necessarily exist.
$ _ FILES ['userfile'] ['SIZE'] size of the uploaded file (in bytes ).
$ _ FILES ['userfile'] ['tmp _ name'] temporary file name stored on the server after the file is uploaded.
$ _ FILES ['userfile'] ['error'] error Code related to the file upload. If the upload is successful, the value is 0.
After the file is uploaded, it is stored in the default temporary directory of the server by default. in php. ini, upload_tmp_dir is set to another path.
Here we have to talk about a move_uploaded_file () function:
This function checks and ensures that the file specified by the file is a valid file to be uploaded (that is, the file is uploaded through the http post upload mechanism of PHP ). If the file is valid, it is moved to the file specified by newloc.
If the file is not a valid Upload file, no operation is performed. move_uploaded_file () returns false.
If the file is valid but cannot be moved for some reason, no operation will occur. move_uploaded_file () will return false, and a warning will be issued.
This check is especially important. If the uploaded file may display the content to the user or other users in the system.
The following is an example of php file upload:
<B> process uploaded files </B> <? Phpif (isset ($ _ FILES ['userfile']) {$ uploaddir = 'upload/'; $ uploadfile = $ uploaddir. basename ($ _ FILES ['userfile'] ['name']); echo '<pre> '; if (move_uploaded_file ($ _ FILES ['userfile'] ['tmp _ name'], $ uploadfile) {echo 'file uploaded successfully '. '<br>';} else {echo 'file Upload Failed '. '<br>';} echo ': some information about the file to be uploaded :'. '<br>'; print_r ($ _ FILES); echo '<pre>'; die () ;}?> <B> upload a form </B> <! -- Enctype in the form, must be consistent with the following definitions --> <form enctype = "multipart/form-data" action = "upload_file.php" name = "upload_form" method = "post"> <! -- MAX_FILE_SIZE must be before all input. If you want to use an upload form in the future, you can write the hidden input --> <input type = "hidden" name = "MAX_FILE_SIZE" value = ""/> file after form: <input type = "file" name = "userfile"/>