模板:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>
多文件上传
</title>
</head>
<body>
<form role="form" action="" method="post" enctype="multipart/form-data">
<div>
文件1:
<input type="file" name="files[]"/>
</div>
<div>
文件2:
<input type="file" name="files[]"/>
</div>
<div>
<button type="submit">上传</button>
</div>
</form>
</body>
</html>
控制器:
<?php
namespace app\admin\controller;
use think\Controller;
/**
*
*/
class Test extends Controller
{
public function index()
{
if (request()->isPost()) {
$files = request()->file('files');
if (!$files) {
return $this->error('请上传文件');
}
foreach ($files as $key => $file) {
$info = $file->move(ROOT_PATH . 'public/uploads');
if ($info) {
// 文件后缀
dump($info->getExtension());
// 保存目录
dump($info->getSaveName());
// 保存的文件名
dump($info->getFileName());
}
}
} else {
return view('index');
}
}
}