← 返回博客
tp52017-06-19 09:40:12 9455

tp5上传多文件

示例说明tp5如何上传多个文件

模板:

<!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');
        }
    }
}

相关推荐