Php 简明教程
PHP - File Uploading
典型 PHP Web 应用程序所需的一个常见功能是允许用户上传文件。在 PHP 中从客户端上传文件非常容易。在本章中,我们将学习如何针对文件上传过程使用 PHP 脚本。
One of the common features required in a typical PHP web application is the provision of letting the user upload files. Uploading files from a client is very easy in PHP. In this chapter, we shall learn how to use PHP script for the file upload process.
上传文件的过程遵循以下步骤 −
The process of uploading a file follows these steps −
-
The user opens the page containing a HTML form featuring a text files, a browse button and a submit button.
-
The user clicks the browse button and selects a file to upload from the local PC.
-
The full path to the selected file appears in the text filed then the user clicks the submit button.
-
The selected file is sent to the temporary directory on the server.
-
The PHP script that was specified as the form handler in the form’s action attribute checks that the file has arrived and then copies the file into an intended directory.
-
The PHP script confirms the success to the user.
为了执行此活动,我们必须首先确保“php.ini”中启用了与文件上传相关的配置设置。
In order to perform this activity, we must first ensure that configuration settings related to file upload are enabled in "php.ini".
打开“php.ini”文件并确保以下设置已启用,方法是在 file_uploads、upload_tmp_dir、upload_max_filesize 和 max_file_uploads 参数中删除前导分号 (;) 符号 −
Open the "php.ini" file and ensure that the following settings are enabled by removing the leading semicolon (;) symbol in file_uploads, upload_tmp_dir, upload_max_filesize and max_file_uploads parameters −
;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;
; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads=On
; Temporary directory for HTTP uploaded files (will use system
; default if not specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir="C:\xampp\tmp"
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize=40M
; Maximum number of files that can be uploaded via a single request
max_file_uploads=20
临时位置和最终位置的文件夹必须设置有允许文件写入的权限。如果任一位置设置为只读,那么进程将失败。
It is necessary that the folders for both temporary and final locations have permissions set that enable file writing. If either is set to be read-only then process will fail.
Creating a File Upload Form
下一步,我们需要设计一个用于文件上传的 HTML 表单。该表单的 method 属性必须为 POST,enctype 必须为 multipart/form-data。使用 file 作为 input 类型,以便让用户浏览并选择要上传的文件。
Next, we need to design a HTML form for file upload. The form’s method attribute must be POST and enctype must be multipart/form-data. Use the input type as file to let the user browse and select the file to be uploaded.
<h2>File Upload Form</h2>
<form method = "POST" action = "uploadfile.php" enctype="multipart/form-data">
<label for="file">File name:</label>
<input type="file" name="uploadfile" />
<input type="submit" name="submit" value="Upload" />
</form>
Creating an Upload Script
uploadfile.php 脚本接收已上传的文件。文件数据收集在超级全局变量 $_FILES 中。获取已上传文件的名称、文件类型、大小和 tmp_name 属性。
The uploadfile.php script receives the uploaded file. The file data is collected in a suparglobal variable $_FILES. Fetch the name, file type, size and the tmp_name attributes of the uploaded file.
move_uploaded_file() 函数将所选文件复制到文档文件夹中。
The move_uploaded_file() function copies the selected file to the document folder.
<?php
echo "<b>File to be uploaded: </b>" . $_FILES["uploadfile"]["name"] . "<br>";
echo "<b>Type: </b>" . $_FILES["uploadfile"]["type"] . "<br>";
echo "<b>File Size: </b>" . $_FILES["uploadfile"]["size"]/1024 . "<br>";
echo "<b>Store in: </b>" . $_FILES["uploadfile"]["tmp_name"] . "<br>";
if (file_exists($_FILES["uploadfile"]["name"])){
echo "<h3>The file already exists</h3>";
} else {
move_uploaded_file($_FILES["uploadfile"]["tmp_name"], $_FILES["uploadfile"]["name"]);
echo "<h3>File Successfully Uploaded</h3>";
}
?>
假设 myform.php 和 uploadfile.php 这两个文件都存储在文档文件夹中。
Assuming that both the files myform.php and uploadfile.php are stored in the document folder.
在浏览器中打开 “myform.php” - ([role="bare"]http://localhost/myform.php) -
Open "myform.php" in the browser ([role="bare"]http://localhost/myform.php) −
单击 File 按钮,浏览到待上传的目标文件,并单击 Upload 按钮。
Click the File button, browse to the desired file to be uploaded, and click the Upload button.
服务器返回以下消息 -
The server responds with the following message −