This is an era of forms. . .
When we edit our own information in the browser, we 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.
<span>1</span> <span><</span><span>form </span><span>enctype</span><span>="multipart/form-data"</span><span> action</span><span>="upload_file.php"</span><span> name</span><span>="upload_form"</span><span> method</span><span>="post"</span><span>></span> <span>2</span> <span><!--</span><span>MAX_FILE_SIZE必须在所有的input之前,以后要是想用上传表单,可以在form之后就写隐藏的input</span><span>--></span> <span>3</span> <span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="MAX_FILE_SIZE"</span><span> value</span><span>="30000"</span><span>/></span> <span>4</span> <span> 上传的文件: </span><span>5</span> <span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="userfile"</span><span>/></span> <span>6</span> <span><</span><span>hr</span><span>/></span> <span>7</span> <span><</span><span>input </span><span>type</span><span>="submit"</span><span> name</span><span>="sub_button"</span><span> value</span><span>="上传文件的提交按钮"</span><span>/></span> <span>8</span> <span>9</span> <span></</span><span>form</span><span>></span>
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: 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 file size 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'] $_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 exist yet.
$_FILES['userfile']['size'] The size of the uploaded file (in bytes).
$_FILES['userfile']['tmp_name'] The temporary file name stored on the server after the file is uploaded.
<$> $ _files ['userfile'] ['error'] and the file upload related error code. 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, 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 to ensure that the file specified by
file is a valid upload file (i.e. uploaded via PHP's HTTP POST upload mechanism). If the file is legal, move it to the file specified by newloc.
Iffile is not a legal uploaded file, no operation will occur and move_uploaded_file() will return false.
Iffile is a legitimate uploaded file but cannot be moved for some reason, no action will be taken and move_uploaded_file() will return false and a warning will be issued.
This 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 uploading files in php:
<span> 1</span> <span><</span><span>b</span><span>></span>上传文件处理<span></</span><span>b</span><span>></span> <span> 2</span> <span><</span><span>hr</span><span>/></span> <span> 3</span> <span><?</span><span>php </span><span> 4</span> <span>if (isset($_FILES['userfile'])) { </span><span> 5</span> <span> $uploaddir = 'upload/'; </span><span> 6</span> <span> $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); </span><span> 7</span> <span> echo '<pre class="brush:php;toolbar:false">'; </span><span> 8</span> <span> if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { </span><span> 9</span> <span> echo '上传文件成功'.'<br>'; </span><span>10</span> <span> } else { </span><span>11</span> <span> echo '上传文件失败'.'<br>'; </span><span>12</span> <span> } </span><span>13</span> <span> echo '这是上传文件的一些信息:' . '<br>'; </span><span>14</span> <span> print_r($_FILES); </span><span>15</span> <span> echo '<pre class="brush:php;toolbar:false">'; </span><span>16</span> <span> die(); </span><span>17</span> <span>} </span><span>18</span> <span>19</span> <span>?></span> <span>20</span> <span><</span><span>b</span><span>></span>上传表单<span></</span><span>b</span><span>></span> <span>21</span> <span><!--</span><span>表单中的enctype属,必须和以下定义保持一致</span><span>--></span> <span>22</span> <span><</span><span>form </span><span>enctype</span><span>="multipart/form-data"</span><span> action</span><span>="upload_file.php"</span><span> name</span><span>="upload_form"</span><span> method</span><span>="post"</span><span>></span> <span>23</span> <span><!--</span><span>MAX_FILE_SIZE必须在所有的input之前,以后要是想用上传表单,可以在form之后就写隐藏的input</span><span>--></span> <span>24</span> <span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="MAX_FILE_SIZE"</span><span> value</span><span>="30000"</span><span>/></span> <span>25</span> <span> 上传的文件: </span><span>26</span> <span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="userfile"</span><span>/></span> <span>27</span> <span><</span><span>hr</span><span>/></span> <span>28</span> <span><</span><span>input </span><span>type</span><span>="submit"</span><span> name</span><span>="sub_button"</span><span> value</span><span>="上传文件的提交按钮"</span><span>/></span> <span>29</span> <span></</span><span>form</span><span>><br /></span>