In php, "$_files" is a predefined array used to obtain information related to files uploaded through the POST method, including the original name of the file, the MIME type of the file, the size of the uploaded file, The temporary file name stored on the server after the file is uploaded, and the error code related to the file upload.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
PHP $_FILES
is a predefined array used to obtain information about files uploaded through the POST method. If uploading a single file, $_FILES is a two-dimensional array; if uploading multiple files, $_FILES is a three-dimensional array.
The contents of the array come from the following sample form. We assume that the name of the file upload field is userfile as shown in the example below. The name can be whatever you want.
$_FILES['userfile']['name']
The original name of the client machine file.
$_FILES['userfile']['type']
The MIME type of the file, if the browser provides this information. An example is "image/gif". However, this MIME type is not checked on the PHP side, so don't take it for granted.
$_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']
The error code related to the file upload.
Example:
Create a file.html demonstration upload file, the code is as follows:
<html> <head></head> <body></body> <form enctype="multipart/form-data" action="file.php" method="POST"> Send this file: <input name="userfile" type="file" /> <input type="submit" value="Send File" /> </form> </html>
Create a new user For the PHP file file.php that receives file information, the code is as follows:
<?php echo "<pre class="brush:php;toolbar:false">"; print_r($_FILES); ?>
After selecting the file on the file.html page, click the Send File button, and the following information will be output on the page:
Array ( [userfile] => Array ( [name] => Screen Shot 2020-05-12 at 18.13.24.png [type] => image/png [tmp_name] => /private/var/tmp/phplVHp3W [error] => 0 [size] => 344925 ) )
Recommended learning: "PHP video tutorial"
The above is the detailed content of How to use php $_files. For more information, please follow other related articles on the PHP Chinese website!