Php 简明教程
PHP - $_FILES
$_FILES 是 PHP 中的“超全局”或自动全局变量之一。它在脚本的整个范围内都可用。$_FILES 变量是一个关联数组,包含通过 HTTP POST 方法上传的项目。
$_FILES is one of the 'superglobal', or automatic global, variables in PHP. It is available in all scopes throughout a script. The variable $_FILES is an associative array containing items uploaded via HTTP POST method.
当 HTML 表单包含文件类型的输入元素,其 enctype 属性设置为 multipart/form-data,且方法属性设置为 HTTP POST 方法时,便会上传文件。
A file is uploaded when a HTML form contains an input element with a file type, its enctype attribute set to multipart/form-data, and the method attribute set to HTTP POST method.
$HTTP_POST_FILES 也包含相同的信息,但它不是超全局的,并且现在已被弃用。
$HTTP_POST_FILES also contains the same information, but it is not a superglobal, and it has now been deprecated.
以下 HTML 脚本包含一个 input 元素的 file 类型的表单——
The following HTML script contains a form with input element of file type −
<input type="file" name="file">
这个“输入类型”渲染出一个带有文件标题的按钮。点击时,会出现一个文件对话框。您可以选择要上传的文件。
This "input type" renders a button captioned as file. When clicked, a file dialogbox pops up. You can choose a file to be uploaded.
服务器上的 PHP 脚本可以访问 $_FILES 变量中的文件数据。
The PHP script on the server can access the file data in $_FILES variable.
$_FILES 数组包含以下属性−
The $_FILES array contains the following properties −
-
$_FILES['file']['name'] − The original name of the file that the user has chosen to be uploaded.
-
$_FILES['file']['type'] − The mime type of the file. An example would be "image/gif". This mime type is however not checked on the PHP side.
-
$_FILES['file']['size'] − The size, in bytes, of the uploaded file.
-
$_FILES['file']['tmp_name'] − The temporary filename of the file in which the uploaded file was stored on the server.
-
$_FILES['file']['full_path'] − The full path as submitted by the browser. Available as of PHP 8.1.0.
-
$_FILES['file']['error'] − The error code associated with this file upload.
error codes 如下枚举−
The error codes are enumerated as below −
Error Codes |
Description |
UPLOAD_ERR_OK (Value=0) |
There is no error, the file uploaded with success. |
UPLOAD_ERR_INI_SIZE (Value=1) |
The uploaded file exceeds the upload_max_filesize directive in php.ini. |
UPLOAD_ERR_FORM_SIZE (Value=2) |
The uploaded file exceeds the MAX_FILE_SIZE. |
UPLOAD_ERR_PARTIAL (Value=3) |
The uploaded file was only partially uploaded. |
UPLOAD_ERR_NO_FILE (Value=4) |
No file was uploaded. |
UPLOAD_ERR_NO_TMP_DIR (Value=6) |
Missing a temporary folder. |
UPLOAD_ERR_CANT_WRITE (Value=7) |
Failed to write file to disk. |
UPLOAD_ERR_EXTENSION (Value=8) |
A PHP extension stopped the file upload. |
Example
以下“test.html”包含一个 HTML 表单,其 enctype 设置为 multiform/form-data。它还具有一个输入文件元素,该元素在表单上显示一个按钮,供用户选择要上传的文件。将此文件保存在 Apache 服务器的文档根目录中。
The following "test.html" contains a HTML form whose enctype is set to multiform/form-data. It also has an input file element which presents a button on the form for the user to select file to be uploaded. Save this file in the document root folder of your Apache server.
<html>
<body>
<form action="hello.php" method="POST" enctype="multipart/form-data">
<p><input type="file" name="file"></p>
<p><input type ="submit" value="submit"></p>
</form>
</body>
</html>
上述 HTML 在浏览器窗口中渲染一个名为“选择文件”的按钮。要打开文件对话框,请单击“选择文件”按钮。当出现所选文件的名称时,请单击 submit 按钮。
The above HTML renders a button named "Choose File" in the browser window. To open a file dialog box, click the "Choose File" button. As the name of selected file appears, click the submit button.
Example
文档根目录中的服务器端 PHP 脚本 ( upload.php ) 如下读取变量 $_FILES 数组−
The server-side PHP script (upload.php) in the document root folder reads the variables $_FILES array as follows −
<?php
echo "Filename: " . $_FILES['file']['name']."<br>";
echo "Type : " . $_FILES['file']['type'] ."<br>";
echo "Size : " . $_FILES['file']['size'] ."<br>";
echo "Temp name: " . $_FILES['file']['tmp_name'] ."<br>";
echo "Error : " . $_FILES['file']['error'] . "<br>";
?>
它将生成以下 output −
It will produce the following output −
Filename: abc.txt
Type : text/plain
Size : 556762
Temp name: C:\xampp\tmp\phpD833.tmp
Error : 0
Example
在 PHP 中,您可以使用 HTML 数组功能上传多个文件 −
In PHP, you can upload multiple files using the HTML array feature −
<html>
<body>
<form action="hello.php" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]"/>
<input type="file" name="files[]"/>
<input type ="submit" value="submit"/>
</form>
</body>
</html>
现在,将 PHP 脚本 ( hello.php ) 更改为 −
Now, change the PHP script (hello.php) to −
<?php
foreach ($_FILES["files"]["name"] as $key => $val) {
echo "File uploaded: $val <br>";
}
?>
浏览器将显示多个“选择文件”按钮。在您通过单击“提交”按钮上传选定的文件后,浏览器将显示名称为文件,以响应 URL http://localhost/hello.html ,如下图所示 −
The browser will show multiple "Choose File" buttons. After you upload the selected files by clicking the "Submit" button, the browser will show the names of files in response to the URL http://localhost/hello.html as shown below −