HTMl代码: <!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]); } }
列表HTML代码:
<!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>