Home >Backend Development >PHP Tutorial >PHP comprehensively uses array functions to achieve multiple file uploads

PHP comprehensively uses array functions to achieve multiple file uploads

黄舟
黄舟Original
2017-05-04 11:46:362712browse

In PHP program development, file upload is a very commonly used function and is also one of the necessary skills for PHP programmers. Fortunately, implementing the file upload function in PHP is much simpler than in languages ​​such as Java and C#. Below we combine specific code examples to introduce in detail how to implement file upload and multi-file upload functions through PHP.

To use PHP to implement the file upload function, we first write two PHP files: index.php and upload.php. Among them, the index.php page is used to submit the form request for file upload, and the upload.php page is used to receive the uploaded file and process it accordingly.

First of all, let’s write a simple index.php file. Since it mainly involves html code, it is relatively simple, so we won’t go into details. The detailed code of the index.php page is as follows:

<?php
//设置编码为UTF-8,以避免中文乱码
header(&#39;Content-Type:text/html;charset=utf-8&#39;);
?>
<!DOCTYPE html>
<html>
<head>
    <title>文件上传表单页面</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
    文件1:<input name="upload_file1" type="file" /><br/>
    文件2:<input name="upload_file2" type="file" /><br/>
    <input type="submit" value="上传" />
</form>
</body>
</html>

It is worth noting that since the HTTP protocol was originally designed, it did not support the file upload function. The default value of the encrypt attribute of the form form is application/x-www-form-urlencoded, which can only be used to submit general Form request. If the submitted form contains files that need to be uploaded, we need to change the enctype attribute value to multipart/form-data to implement the file upload function. In addition, the method attribute value must be post.

Next, we continue to write the code for the upload.php file.

<?php
//设置编码为UTF-8,以避免中文乱码
header(&#39;Content-Type:text/html;charset=utf-8&#39;);
$first_file = $_FILES[&#39;upload_file1&#39;];  //获取文件1的信息
$second_file = $_FILES[&#39;upload_file2&#39;]; //获取文件2的信息
$upload_dir = &#39;D:/upload/&#39;;   //保存上传文件的目录//处理上传的文件1
if ($first_file[&#39;error&#39;] == UPLOAD_ERR_OK){
    //上传文件1在服务器上的临时存放路径
    $temp_name = $first_file[&#39;tmp_name&#39;];
    //上传文件1在客户端计算机上的真实名称
    $file_name = $first_file[&#39;name&#39;];
    //移动临时文件夹中的文件1到存放上传文件的目录,并重命名为真实名称
    move_uploaded_file($temp_name, $upload_dir.$file_name);
    echo &#39;[文件1]上传成功!<br/>&#39;;
}else{
    echo &#39;[文件1]上传失败!<br/>&#39;;
}
//处理上传的文件2
if ($second_file[&#39;error&#39;] == UPLOAD_ERR_OK){
    //上传文件2在服务器上的临时存放路径
    $temp_name = $second_file[&#39;tmp_name&#39;];
    //上传文件2在客户端计算机上的真实名称
    $file_name = $second_file[&#39;name&#39;];
    //移动临时文件夹中的文件2到存放上传文件的目录,并重命名为真实名称
    move_uploaded_file($temp_name, $upload_dir.$file_name);
    echo &#39;[文件2]上传成功!<br/>&#39;;
}else {
    echo &#39;[文件2]上传失败!<br/>&#39;;
}
?>

In PHP, when the form request submitted by the browser client contains an uploaded file, PHP will temporarily store the uploaded file in a temporary directory (in Windows operating systems, the default The temporary directory is generally C:/Windows/Temp), and then the relevant information of the uploaded file is stored in the super global variable $_FILES. Therefore, we only need to obtain the uploaded file information through the $_FILES array, and then perform corresponding processing operations on it. Next, let’s take a look at the details of using the print_r() function to output the super global variable $_FILES when uploading two image files A.gif and B.gif through the browser:

Array ( [upload_file1] => Array ( 
		[name] => A.gif (客户端上传时的真实文件名称)
		[type] => image/gif (文件的类型)
		[tmp_name] => C:\Windows\Temp\php9803.tmp (文件上传到PHP服务器后临时存放的路径)
		[error] => 0 (错误信息,0表示没有错误)
		[size] => 87123 (文件大小,单位为字节)
		)
        [upload_file2] => Array (
		[name] => B.gif
		[type] => image/gif
		[tmp_name] => C:\Windows\Temp\php9813.tmp
		[error] => 0
		[size] => 93111
		)
)

In the above example , the parameter names of the two files we uploaded are upload_file1 and upload_file2. Now, we let multiple files in the form use the same parameter name upload_file, and resubmit the two files just uploaded in the form of parameter arrays for upload. At this time, we need to modify the two file file fields in the index.php page to the following html code:

文件1:<input name="upload_file[]" type="file" /><br/>
文件2:<input name="upload_file[]" type="file" /><br/>

In addition, we also need to make corresponding modifications to the upload.php page:

<?php
//设置编码为UTF-8,以避免中文乱码
header(&#39;Content-Type:text/html;charset=utf-8&#39;);
$fileArray = $_FILES[&#39;upload_file&#39;];//获取多个文件的信息,注意:这里的键名不包含[]
$upload_dir = &#39;D:/upload/&#39;; //保存上传文件的目录
foreach ( $fileArray[&#39;error&#39;] as $key => $error) {
    if ( $error == UPLOAD_ERR_OK ) { //PHP常量UPLOAD_ERR_OK=0,表示上传没有出错
        $temp_name = $fileArray[&#39;tmp_name&#39;][$key];
        $file_name = $fileArray[&#39;name&#39;][$key];
        move_uploaded_file($temp_name, $upload_dir.$file_name);
        echo &#39;上传[文件&#39;.$key.&#39;]成功!<br/>&#39;;
    }else {
        echo &#39;上传[文件&#39;.$key.&#39;]失败!<br/>&#39;;
    }
}
?>

Similarly, we use the print_r() function to view the detailed information of the super global variable $_FILES in the above example:

Array ( 
	[upload_file] => Array ( 
		[name] => Array ( 
			[0] => A.gif
			[1] => B.gif			
			) 
		[type] => Array ( 
			[0] => image/gif
			[1] => image/gif			
			) 
		[tmp_name] => Array (
			[0] => C:\Windows\Temp\php87B9.tmp
			[1] => C:\Windows\Temp\php87BA.tmp
			) 
		[error] => Array ( 
			[0] => 0
			[1] => 0			
			) 
		[size] => Array ( 
			[0] => 87123
			[1] => 93111			
			)
		)
)

Note 1: Under the default configuration of PHP, the size of the uploaded file An error will occur if it exceeds a certain range

Note 2: The above PHP code for processing file uploads is just a simple introductory example and cannot be used directly as formal code because there are many additional requirements. Note that security factors such as file type, file size, and duplicate names of uploaded files are not taken into consideration.

Note 3: If the uploaded file name contains Chinese characters, it may cause garbled file names. At this time, you need to use the function iconv() to convert the encoding of the file name.


##【Related tutorial recommendations】

1. Relevant topic recommendations: "

php array (Array)"



The above is the detailed content of PHP comprehensively uses array functions to achieve multiple file uploads. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn