Let's talk about how to process uploaded files in php,
This is an era of forms...
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.
1 <form enctype = "multipart/form-data" action = "upload_file.php" name = "upload_form" method = "post"> 2 <! -- 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 --> 3 <input type = "hidden" name = "MAX_FILE_SIZE" value = "30000"/> 4 after form: 5 <input type = "file" name = "userfile"/> 6
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 thatFileThe specified file is a valid Upload File (that is, it is uploaded through the http post upload mechanism of PHP ). If the file is valid, move itNewlocThe specified file.
IfFileIf the file is not uploaded legally, no operation is performed. move_uploaded_file () returns false.
IfFileIt is a valid file to be uploaded, but it cannot be moved for some reason and 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:
1 <B> process uploaded files </B> 2