欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PHP多name值多文件上传

程序员文章站 2022-05-30 20:42:11
...

PHP多name值多文件上传

前端使用的时bootstrap框架
后端使用php

前端页面效果展示

PHP多name值多文件上传

前端代码

<div class="form-group row col-md-12" style="border-bottom: 2px solid #eee;" id="allonm">
	<label  class="col-sm-6 col-form-label">附录1</label>
	<div class="input-group col-sm-6">
		<div class="col-sm-6">
			 <label for="objects_num_money">
	        	<span id="onmtext"></span> 
				<input type="file" id="objects_num_money" name="objects_num_money[]">
			</label> 
		</div>
		<div class="col-sm-6">
			<div style="" class="btn btn-primary" onclick="addonm()">
				<a style="color: #fff;text-decoration:none;">添加</a>
			</div>
		</div>
	</div>
</div>
<div class="form-group row col-md-12" style="border-bottom: 2px solid #eee;" id="allonm">
	<label  class="col-sm-6 col-form-label">附录2</label>
	<div class="input-group col-sm-6">
		<div class="col-sm-6">
			 <label for="org_city">
	        	<span id="octext"></span> 
				<input type="file" id="org_city" name="org_city[]">
			</label> 
		</div>
		<div class="col-sm-6">
			<div style="" class="btn btn-primary" onclick="addoc()">
				<a style="color: #fff;text-decoration:none;">添加</a>
			</div>
		</div>
	</div>
</div>

js代码

function addonm(){
		var str = ' <div class="col-sm-6"></div><div class="input-group col-sm-6"><div class="col-sm-6"><input type="file"  name="objects_num_money[]" style=""></div><div class="col-sm-6"><div style="" class="btn btn-primary" οnclick="deladd(this)"><a style="color: #fff;text-decoration:none;">删除</a></div></div> '
		$('#allonm').append(str)
	}
	function addco(){
		var str = ' <div class="col-sm-6"></div><div class="input-group col-sm-6"><div class="col-sm-6"><input type="file"  name="city_org[]" style=""></div><div class="col-sm-6"><div style="" class="btn btn-primary" οnclick="deladd(this)"><a style="color: #fff;text-decoration:none;">删除</a></div></div> '
		$('#allco').append(str)
	}

php代码

function test(){
	   $data = $_FILES;
	  
	   for ($i=0,$len=count($data['objects_num_money']['name']);$i<$len;$i++){
	    //遍历循环获取单个文件
		    $file=array(
		      	'name'=>$data['objects_num_money']['name'][$i],
		    	'type'=>$data['objects_num_money']['type'][$i],
		        'tmp_name'=>$data['objects_num_money']['tmp_name'][$i],
		        'error'=>$data['objects_num_money']['error'][$i],
		        'size'=>$data['objects_num_money']['size'][$i]
		    );
		    if ($file["error"] == 0) {
		    //单个文件移动
				  $imgname =$path.'/附件1-'.$file['name'];
			     $bol = move_uploaded_file($file["tmp_name"], $imgname);
			     //数组追加
			     array_push($onm, $imgname);
			}
		}
		//附录一的字符串文件储存路径
		$onm = implode(',', $onm);
		$co = array();
		for ($i=0,$len=count($data['city_org']['name']);$i<$len;$i++){
		    $file=array(
		      	'name'=>$data['city_org']['name'][$i],
		    	'type'=>$data['city_org']['type'][$i],
		        'tmp_name'=>$data['city_org']['tmp_name'][$i],
		        'error'=>$data['city_org']['error'][$i],
		        'size'=>$data['city_org']['size'][$i]
		    );
		    if ($file["error"] == 0) {
				 $imgname =$path.'/附件2-'.$file['name'];
			     $bol = move_uploaded_file($file["tmp_name"], $imgname);
			     array_push($co, $imgname);
			}
		}
		//附录二的字符串文件储存路径
		$co = implode(',', $co);
		$model = new test();
		//最后传到数据库
		$model->objects_num_money = $onm;
    	$model->city_org = $co;
    	$model->save();
  }