$_FILES数组(HTTP文件上传变量)
PHP文件句柄如何知道一些基本信息,如文件名称、文件大小、文件类型以及已选择上传的文件的属性?让我们看看幕后发生了什么。
$_FILES是通过HTTP POST方法上传的二维关联全局项数组,它包含以下文件的属性:
Attribute | Description |
---|---|
[name] | Name of file which is uploading |
[size] | Size of the file |
[type] | Type of the file (like .pdf, .zip, .jpeg…..etc) |
[tmp_name] | A temporary address where the file is located before processing the upload request |
[error] | Types of error occurred when the file is uploading |
现在看看这个数组是什么样的??
- $_FILES[input-field-name][name]
- $_FILES[input-field-name][tmp_name]
- $_FILES[input-field-name][size]
- $_FILES[input-field-name][type]
- $_FILES[input-field-name][error]
让我们来看一些例子,在第一个例子中这个数组是如何工作的。
1 2 3 4 5 6 7 8 9 | <?php echo "<pre>"; print_r($_FILES); echo "</pre>"; ?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="Upload Image"> </form> |
在上面的脚本中,在上传文件之前
一旦我们选择文件并上传,函数print_r将显示PHP超全局关联数组$_FILES的信息。
例2:添加html代码,后面跟着PHP脚本不同的文件。让我们制作一个用于上传文件的HTML表单 index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <!DOCTYPE html> <html> <head> <title>GeeksForGeeks</title> <style type="text/css"> div { background: #4CB974; text-align: center; font-size: 20px; padding: 30px; color: #fff; font-family: sans-serif; } </style> </head> <body> <form action="file-upload-manager.php" method="post" enctype="multipart/form-data" style="border: 1px solid #1f1f1f; padding: 20px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);"> <!--multipart/form-data ensures that form data is going to be encoded as MIME data--> <h2>Upload File</h2> <input type="file" name="photo" id="fileSelect"><br><br> <input type="submit" name="submit" value="Upload"><br><br> <!-- name of the input fields are going to be used in our php script--> <div> This Video is made for GFG</div> </form> </body> </html> |
现在,是时候编写一个能够处理文件上传系统的php脚本了。file-upload-manager.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <?php // Check if the form was submitted if ($_SERVER["REQUEST_METHOD"] == "POST") { // Check if file was uploaded without errors if (isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0) { $file_name = $_FILES["photo"]["name"]; $file_type = $_FILES["photo"]["type"]; $file_size = $_FILES["photo"]["size"]; $file_tmp_name = $_FILES["photo"]["tmp_name"]; $file_error = $_FILES["photo"]["error"]; echo "<div style='text-align: center; background: #4CB974; padding: 30px 0 10px 0; font-size: 20px; color: #fff'> File Name: " . $file_name . "</div>"; echo "<div style='text-align: center; background: #4CB974; padding: 10px; font-size: 20px; color: #fff'> File Type: " . $file_type . "</div>"; echo "<div style='text-align: center; background: #4CB974; padding: 10px; font-size: 20px; color: #fff'> File Size: " . $file_size . "</div>"; echo "<div style='text-align: center; background: #4CB974; padding: 10px; font-size: 20px; color: #fff'> File Error: " . $file_error . "</div>"; echo "<div style='text-align: center; background: #4CB974; padding: 10px; font-size: 20px; color: #fff'> File Temporary Name: " . $file_tmp_name . "</div>"; } } ?> |
在上面的脚本中,一旦提交了表单,稍后我们就可以通过PHP超全局关联数组$_FILES访问信息。除了使用$_FILES数组之外,许多内置函数都扮演了重要角色。上传文件完成后,在脚本中我们将检查服务器的请求方法,如果是POST,则继续,否则系统将抛出一个错误。稍后,我们访问了$_FILES数组以获得文件名、文件大小和文件类型。一旦我们得到了这些信息,使用echo打印文件的信息。
输出
参考
http://php.net/manual/en/reserved.variables.files.php