TP5实现多文件上传及展示

view层上传:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="{:url('imgs/upload')}" method="post" enctype="multipart/form-data">
    <input type="file" name="imgs[]" multiple="multiple">
    <input type="submit" value="提交"/>
</form>
</body>
</html>
controller添加入库:

<?php
namespace app\index\controller;
use think\Controller;
use think\Db;

class Imgs extends Controller
{
    public function imgs(){
        return view();
    }
    public function upload(){
     // 获取表单上传文件
        $files = request()->file('imgs');
        foreach($files as $file){
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            $data[] = '/uploads/'.$info -> getSaveName();
        }
        $arr['imgs'] = implode(',',$data);
//        echo $arr;die;
        $res = Db::name('admin') -> insert($arr);
        if($res){
            $this -> success('添加成功','show');
        }else{
            $this -> error('添加失败');
        }
    }
    public function show(){
        $data = Db::name('admin') -> select();
//        dump($data);die;
        foreach($data as $k => $v){
            $data[$k]['imgs'] = explode(',',$v['imgs']);
        }
//        dump($data);die;
        return view('show',['data' => $data]);
    }
}
view展示:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
{volist name="data" id="vo"}
    {volist name="vo.imgs" id="v"}
    <img src="__ROOT__/{$v}" alt=""/>
    {/volist}
{/volist}
</body>
</html>

 

posted @ 2022-01-08 17:52  王越666  阅读(100)  评论(0编辑  收藏  举报