Home  >  Article  >  Backend Development  >  Detailed explanation of examples of uploaded file processing problems in PHP

Detailed explanation of examples of uploaded file processing problems in PHP

墨辰丷
墨辰丷Original
2018-06-02 09:44:391256browse

This article mainly introduces relevant information on interpreting the processing of uploaded files in PHP. It is very good and has reference value. Friends in need can refer to it

We edit our own information in the browser , you will encounter uploading avatars; in the library, we will upload documents... The word "upload" exists everywhere.

PHP is the best language (programmers in other languages, please don’t hit me...). PHP has natural advantages in handling interactions, and naturally has powerful functions to handle uploaded files.

Just like submitting general data, uploading files also requires a form. Let's create a special form to upload files.

<form enctype="multipart/form-data" action="upload_file.php" name="upload_form" method="post">
<!--MAX_FILE_SIZE必须在所有的input之前,以后要是想用上传表单,可以在form之后就写隐藏的input-->
<input type="hidden" name="MAX_FILE_SIZE" value=""/>
上传的文件:
<input type="file" name="userfile"/>
<input type="submit" name="sub_button" value="上传文件的提交按钮"/>
</form>

OK, let’s analyze this code snippet.

The above enctype specifies the encoding format used when the data is sent to the server. It has three values:

MAX_FILE_SIZE hidden field (unit is bytes) must be placed before the file input field, and its value is the maximum size of the file. This is a suggestion for browsers, PHP will also check this. This barrier can be bypassed on the browser side, so don't expect to use it to block large files. However, the maximum value of the file is limited by post_max_size= (number)M in php.ini. But it is better to add this item, which can avoid the trouble of users spending time waiting to upload large files only to find that large file upload failed.

After the user submits the file form, the server can accept the data. There is a global variable $_FILES in PHP to process files. It is assumed that the upload field name is userfile (can be changed at will in the field).

 $_FILES['userfile']['name'] The original name of the client file.
 $_FILES['userfile']['type'] The MIME type of the file. This is not checked on the PHP side, so this value may not be available yet.
 $_FILES['userfile']['size'] The size of the uploaded file (in bytes).
         $_FILES['userfile']['tmp_name'] The temporary file name stored on the server side after the file is uploaded.
 $_FILES['userfile']['error'] The 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 server's default temporary directory by default, and the upload_tmp_dir in php.ini 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 file is a legal uploaded file (i.e. Uploaded via PHP's HTTP POST upload mechanism). If the file is legal, it is moved to the file specified by newloc.

If file is not a legal uploaded file, no operation will occur and move_uploaded_file() will return false.

If file is a legitimate uploaded file but cannot be moved for some reason, no action will occur and move_uploaded_file() will return false and a warning will be issued.

This kind of check is particularly important if the uploaded file may cause its content to be displayed to the user or other users of this system.

The following is an example of a php upload file:

<b>上传文件处理</b>
<?php
if (isset($_FILES[&#39;userfile&#39;])) {
$uploaddir = &#39;upload/&#39;;
$uploadfile = $uploaddir . basename($_FILES[&#39;userfile&#39;][&#39;name&#39;]);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
if (move_uploaded_file($_FILES[&#39;userfile&#39;][&#39;tmp_name&#39;], $uploadfile)) {
echo &#39;上传文件成功&#39;.&#39;<br>&#39;;
} else {
echo &#39;上传文件失败&#39;.&#39;<br>&#39;;
}
echo &#39;这是上传文件的一些信息:&#39; . &#39;<br>&#39;;
print_r($_FILES);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
die();
}
?>
<b>上传表单</b>
<!--表单中的enctype属,必须和以下定义保持一致-->
<form enctype="multipart/form-data" action="upload_file.php" name="upload_form" method="post">
<!--MAX_FILE_SIZE必须在所有的input之前,以后要是想用上传表单,可以在form之后就写隐藏的input-->
<input type="hidden" name="MAX_FILE_SIZE" value=""/>
上传的文件:
<input type="file" name="userfile"/>
<hr/>
<input type="submit" name="sub_button" value="上传文件的提交按钮"/>
</form>

Summary: The above is the entire content of this article, I hope it can be helpful to everyone’s study .

Related recommendations:

phpMethods for downloading various files based on the header function

PHP implements creation and compression Picture method

phpExample sharing of single file and multiple file uploads

The above is the detailed content of Detailed explanation of examples of uploaded file processing problems in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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