TP框架实现存放在单个字段的多图片路径修改
之前做项目开发时候碰到一种将很多图片的路径存放在,如下图:
就是图片路径中规则插入--用来区分。
在html页面的代码部分:
<div id="imgs" style="width:90%;margin:0 auto; display:none;"> <?php $imgs = explode("--",$info['htimg']); foreach($imgs as $k=>$i){ ?> <form method='post' enctype="multipart/form-data" action="<?php echo U('/Info/All/upimg/')?>"> <img id="images" src="<?php echo $i;?>" style="width:30%;margin-bottom:20px;height: 200px;" > <input type='file' multiple="true" name="img[]" id="htimg" style="height:26px;width:200px;border:1px solid #ccc;padding-left:5px;" /> <input type="hidden" class="btn btn-primary" name="xu" value="<?php echo $k;?>"/> <input type="hidden" class="btn btn-primary" name="id" value="<?php echo $info['id'];?>"/> <input type="submit" class="btn btn-primary" style="margin-left:30px;height:30px;width:90px;line-height:25px;" value="上传"> </form> <?php }} ?> </div>
页面效果:
因为图片的路径是存放在同一个字段时候,如果修改某一个张图片,整个部分都会受到影响,后面用了一些方法终于是实现了局部的页面修改,代码如下:
$odata['id'] = $id; $img = $_FILES['img']; $image = new \Think\Image(); $y = date("Y"); $t = substr(time(),2); //图片路径 $targetPath = './Public/images/'.$y.'/'.date('md'); $dir = ''; foreach(explode('/',$targetPath) as $p){ $dir = $dir.$p."/"; $n++; if(!file_exists($dir)){ @mkdir($dir); } } foreach($img['tmp_name'] as $k=>$ht){ if(!empty($ht)){ $targetFile1 = $targetPath.'/'.$y.$t.rand(10000,99999).'.'.end(explode(".",$img['name'])); move_uploaded_file($ht,$targetFile1); $image->open($targetFile1); $image->thumb(1080, 1080)->save($targetFile1); if(!empty($add)){ $imgs[] = ltrim($targetFile1,'.'); }else{ $imgs = ltrim($targetFile1,'.'); } } } if(empty($imgs)){ echo "<script>alert('未上传照片!');history.back(-1);</script>";exit(); }else{ $ht = explode("--",$order['img']); if(!empty($add)){ $addimg =implode('--',$imgs); if(!empty($order['htimg'])){ $odata['img']=$order['img'].'--'.$addimg ; }else{ $odata['img']=$addimg ; } }else{ $ht[$xu] = $imgs; $odata['img'] = implode('--',$ht); } M("allimg")->save($odata);