This article will continue to introduce how to use the multiple attribute in HTML5 to implement PHP multi-file upload and information parsing.
So what are the simple methods for uploading multiple files in PHP? ], I have briefly introduced some simple methods for uploading multiple files in PHP. Friends who need it can refer to it.
First the HTML form code is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="getFile.php" method="post" enctype="multipart/form-data"> 选择文件进行上传: <input type="file" name="file[]" multiple=""><br> <input type="submit" value="上传"> </form> </body> </html>
## Then upload the file to the getFile.php file. The PHP code example is as follows:
var_dump($_FILES);
This The result when accessed through the browser is as follows:
At this time, you can see that we have obtained the three-dimensional array in the picture above.
So if we want to analyze the information of multiple uploaded files, we need to assemble the uploaded information first.
Then the complete getFile.php code is as follows:<?php
/**
* 组装多文件上传信息
*/
$files = [];
function getFile()
{
$i = 0;
foreach ($_FILES as $file) {
if (is_string($file['name'])) {
$files['$i'] = $file;
$i++;
} elseif (is_array($file['name'])) {
foreach ($file['name'] as $k => $v) {
$files[$i]['name'] = $file['name'][$k];
$files[$i]['type'] = $file['type'][$k];
$files[$i]['tmp_name'] = $file['tmp_name'][$k];
$files[$i]['error'] = $file['error'][$k];
$files[$i]['size'] = $file['size'][$k];
$i++;
}
}
}
return $files;
}
/**
* 文件上传
* @param $fileInfo
* @param string $upload
* @param array $imagesExt
* @return string
*/
function upload_file($fileInfo, $upload = "./upload", $imagesExt = ['gif', 'png', 'jpg'])
{
$res = [];
if ($fileInfo['error'] === 0) {
$ext = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
if (!in_array($ext, $imagesExt)) {
$res['mes'] = "文件非法类型";
}
if (!is_dir($upload)) {
mkdir($upload, 0777, true);
}
if ($res) {
return $res;
}
$fileName = md5(uniqid(microtime(true), true)) . "." . $ext;
$destName = $upload . "/" . $fileName;
if (!move_uploaded_file($fileInfo['tmp_name'], $destName)) {
$res['mes'] = "文件上传失败!";
}
$res['mes'] = $fileInfo['name'] . "文件上传成功!";
$res['dest'] = $destName;
return $res;
} else {
switch ($fileInfo['error']) {
case 1:
$res['mes'] = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
break;
case 2:
$res['mes'] = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
break;
case 3:
$res['mes'] = '文件只有部分被上传';
break;
case 4:
$res['mes'] = '没有文件被上传';
break;
case 6:
$res['mes'] = '找不到临时文件夹';
break;
case 7:
$res['mes'] = '文件写入失败';
break;
}
return $res;
}
}
$files = getFile();
foreach ($files as $fileInfo) {
$res = upload_file($fileInfo);
echo $res['mes'];
var_dump($res['dest']);
}
to check the uploaded Judge the multi-dimensional array informationReorganize, and then create the upload_file method to parse the upload information of multiple files. The upload_file method here has been introduced in detail in this article [
Detailed explanation of PHP file upload method and its information analysis], you can choose to refer to it. The above is the specific method of implementing multi-file upload and information analysis in PHP. If you want to know more about PHP, you can follow the
PHP video tutorialThe above is the detailed content of How to use multiple attribute in PHP to upload multiple files and parse information? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!