thinkphp处理多图上传

多图上传

html上传图片代码

<form method="post" action="{
     {:MyUrl('admin/answer/upload')}}" 	enctype=multipart/form-data>
	<input type="file" name="files[]" multiple/>
	<input type="submit"/>
</form>

接收不到数据时,查看form表单中是否添加enctype=multipart/form-data;
控制器

public function upload(){
     
		$files=request()->file('files');
       	 foreach($files as $item){
     
        	   //移动到框架应用根目录/uploads/目录下
        	   $info = $item->move(ROOT_PATH . 'uploads');
        	   if($info){
     
	               $saveName=str_replace("\\","/",$info->getSaveName());
             	   $img='/uploads/'.$saveName;
      	      }
      	      //字符串转为数组
         	   $data[]= explode(",",$img);
    	    }
         	   $arr=' ';
         	   foreach($data as $k=>$v){
     
             	   $arr.=$v[0].",";
         	   }
    	    $fin= ['files'=>$arr];
			$end=Db::name('answer')->insert($fin);
	}

前端显示多图控制器代码

public function index(){
     
	$data = Db::name('Answer')->where('id' , $params)->find();
		// 消除字段最右边的符号
		$end = rtrim($data['files'],",");
		// 将字符串打散为数组
		$test = explode(',',$end);
		$data['test']=$test;
		  $this->assign('v', $data);
		return $this->fetch();
}

前端页面显示

{
     {
     volist name="$v.test" id="vo" key="k"}}
			<img style="width:550px;height:600px;" src="/public{
     {
       $vo}}" />
	{
     {
     /volist}}

你可能感兴趣的:(thinkphp多图上传)