当图片全部是新增的时候,id和FILE一一对应,后台可以匹配处理。

array(1) {
["banner_img"] => array(5) {
["name"] => array(2) {
[0] => string(5) "2.png"
[1] => string(5) "1.png"
}
["type"] => array(2) {
[0] => string(9) "image/png"
[1] => string(9) "image/png"
}
["tmp_name"] => array(2) {
[0] => string(14) "/tmp/phpO7u01p"
[1] => string(14) "/tmp/phpQZJc7O"
}
["error"] => array(2) {
[0] => int(0)
[1] => int(0)
}
["size"] => array(2) {
[0] => int(176795)
[1] => int(150598)
}
}
}
array(2) {
["pid"] => string(1) "8"
["banner_id"] => array(2) {
[0] => string(0) ""
[1] => string(0) ""
}
}

但是,当图片是修改的时候,有些图片并不需要重新传图,于是就无法一一对应了。比如:

array(1) {
["banner_img"] => array(5) {
["name"] => array(2) {
[0] => string(5) "2.png"
[1] => string(5) "2.png"
}
["type"] => array(2) {
[0] => string(9) "image/png"
[1] => string(9) "image/png"
}
["tmp_name"] => array(2) {
[0] => string(14) "/tmp/phpbmHWr5"
[1] => string(14) "/tmp/phphRfcX9"
}
["error"] => array(2) {
[0] => int(0)
[1] => int(0)
}
["size"] => array(2) {
[0] => int(176795)
[1] => int(176795)
}
}
}
array(3) {
["pid"] => string(2) "10"
["banner_id"] => array(4) {
[0] => string(1) "1"
[1] => string(1) "2"
[2] => string(0) ""
[3] => string(0) ""
}
["banner_img"] => array(2) {
[0] => string(0) ""
[1] => string(0) ""
}
}

有一种办法,就是新图单独组合到banner_img数组中。老图单独处理。

array(2) {
["banner_img_1"] => array(5) {
["name"] => string(5) "3.png"
["type"] => string(9) "image/png"
["tmp_name"] => string(14) "/tmp/phpo2sq7A"
["error"] => int(0)
["size"] => int(171795)
}
["banner_img"] => array(5) {
["name"] => array(2) {
[0] => string(5) "2.png"
[1] => string(5) "2.png"
}
["type"] => array(2) {
[0] => string(9) "image/png"
[1] => string(9) "image/png"
}
["tmp_name"] => array(2) {
[0] => string(14) "/tmp/phpbR50hb"
[1] => string(14) "/tmp/phpPvrGsL"
}
["error"] => array(2) {
[0] => int(0)
[1] => int(0)
}
["size"] => array(2) {
[0] => int(176795)
[1] => int(176795)
}
}
}
array(3) {
["pid"] => string(2) "10"
["banner_id"] => array(4) {
[0] => string(1) "1"
[1] => string(1) "2"
[2] => string(0) ""
[3] => string(0) ""
}
["banner_img_2"] => string(0) ""
}

这里id为1的修改了,id为2的图片没有变动。剩下的两个是新图,跟banner_img中的数据一一对应。

/**
* 保存步骤二
*/
public function save_step_2() {
// 先删除之前的Banner图
M()->startTrans();
$error_count = 0; $pid = $_POST['pid'];
$product_banner = M('product_banner');
$product_banner->where(['pid'=>$pid])->save(['status'=>0]); $temp_all_file = $_FILES;
$temp_new_file = $_FILES['banner_img'];
unset($_FILES);
$temp_banner_id = $_POST['banner_id'];
$cursor_new = 0;
foreach ($temp_banner_id as $k=>$v) {
// 重置$data数据
$data = [];
if ($v) { // 修改
// 可能传图,也可能未传图
if ($temp_all_file['banner_img_'.$v]) { // 传图了
$_FILES['banner_img'] = $temp_all_file['banner_img_'.$v];
$file = $this->upload(1,750,750,'banner_img','banner_img');
$data['img_url'] = $file['save_name'];
if(!$data['img_url']) {
$error_count ++;
break;
}
}
$data['pid'] = $pid;
$data['status'] = 1;
$edit_flag = $product_banner->where(['id'=>$v])->save($data);
if (!$edit_flag && $edit_flag !==0 ) {
$error_count ++;
break;
} } else { // 新增,变更游标
$file_up = array();
$file_up['name'] = $temp_new_file['name'][$cursor_new];
$file_up['type'] = $temp_new_file['type'][$cursor_new];
$file_up['tmp_name'] = $temp_new_file['tmp_name'][$cursor_new];
$file_up['error'] = $temp_new_file['error'][$cursor_new];
$file_up['size'] = $temp_new_file['size'][$cursor_new];
$_FILES['banner_img'] = $file_up; $file = $this->upload(1,750,750,'banner_img','banner_img');
$data['img_url'] = $file['save_name']; if(!$data['img_url']) {
$error_count ++;
break;
} $data['pid'] = $pid;
$data['create_time'] = time();
$data['status'] = 1;
$add_flag = $product_banner->add($data);
if (!$add_flag) {
$error_count ++;
break;
}
unset($_FILES, $file_up);
$cursor_new ++;
}
} if($error_count > 0){
M()->rollback();
$this->json->setErr(10099, '操作失败');
$this->json->Send();
}else{
M()->commit();
$this->json->setErr(0, '操作成功');
$this->json->setAttr('pid', $pid);
$this->json->Send();
}
}

至此,巧妙的处理了FILE与id不对应的问题。

FILE,id不一致的更多相关文章

  1. <input type="file" id="camera" multiple="multiple" capture="camera" accept="image/*"> 上传图片,手机调用相册和摄像头

    <input type="file" id="camera" multiple="multiple" capture="ca ...

  2. 【翻译自mos文章】对于每个文件的 file.id and file.incarnation number,重命名文件别名

    对于每个文件的 file.id and file.incarnation number,重命名文件别名 參考原文: Rename Alias of Datafile as Per file.id an ...

  3. <input type="file" id="fileID">文本框里的值清空方法

    一般情况下,不允许通过脚本来对文件上传框赋值. 下面是一个变通的方法.就是创建一个新的input type="file" 把原来的替换掉. <!DOCTYPE html PU ...

  4. 关于Webdriver自动化测试时,页面数据与数据库id不一致的处理方式,需要使用鼠标事件

    有时候Web页面需要通过onmouseout事件去动态的获取数据库的数据,在使用Webdriver进行自动化测试的时候,对于页面显示的数据,其在数据库可能会存在一个id或者code,但是id或者cod ...

  5. hadoop中集群节点ID不一致( java.io.IOException: Incompatible clusterIDs )

  6. JS调用activeX实现浏览本地文件夹功能 wekit内核只需要<input type="file" id="files" name="files[]" webkitdirectory/>即可,IE内核比较麻烦

    研究了一天,js访问本地文件本身是不可能的,只能借助于插件.植入正题,IE仅支持ActiveX插件. function openDialog() { try { var Message = " ...

  7. 自定义input[type="file"]的样式

    input[type="file"]的样式在各个浏览器中的表现不尽相同: 1. chrome: 2. firefox: 3. opera: 4. ie: 5. edge: 另外,当 ...

  8. input[tyle="file"]样式修改及上传文件名显示

    默认的上传样式我们总觉得不太好看,根据需求总想改成和上下结构统一的风格…… 实现方法和思路: 1.在input元素外加a超链接标签 2.给a标签设置按钮样式 3.设置input[type='file' ...

  9. 前端开发:css技巧,如何设置select、radio 、 checkbox 、file这些不可直接设置的样式 。

    前言: 都说程序员有三宝:人傻,钱多,死得早.博主身边的程序“猿”一大半应了这三宝,这从侧面说明了一个问题,只有理性是过不好日子的.朋友们应该把工作与生活分开,让生活变得感性,让工作变得理性,两者相提 ...

随机推荐

  1. Ubuntu下sublime-text3安装步骤

    1.在Ubuntu中按CTRL+ALT+T打开命令窗口,按下面步骤和命令进行安装即可: 添加sublime text 3的仓库: sudo add-apt-repository ppa:webupd8 ...

  2. java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'waterQuality

    如果一个项目中有两个@RequestMapping("/xxx")完全相同就会报  java.lang.IllegalStateException 改进办法:修改@RequestM ...

  3. modelform save

    ModelForm表单 save()方法 每一个ModelForm都有一个save()方法,这个方法可以更具绑定的form表单创建并且保存一个数据库对象,ModelForm的子类可以接受一个model ...

  4. python之auto鼠标/键盘事件

    mouse_key.py import os import time import win32gui import win32api import win32con from ctypes impor ...

  5. Golang利用select实现超时机制

    所谓超时,比如上网浏览一些安全的网站,如果几分钟之后不做操作,那么就会让你重新登录.就所谓有时候出现goroutine阻塞的情况,那么我们如何避免整个程序进入阻塞情况,这时候就可以用select来设置 ...

  6. Java1.7 HashMap 实现原理和源码分析

    HashMap 源码分析是面试中常考的一项,下面一篇文章讲得很好,特地转载过来. 本文转自:https://www.cnblogs.com/chengxiao/p/6059914.html 参考博客: ...

  7. PowerDesigner 学习:十大模型及五大分类

    个人认为PowerDesigner 最大的特点和优势就是1)提供了一整套的解决方案,面向了不同的人员提供不同的模型工具,比如有针对企业架构师的模型,有针对需求分析师的模型,有针对系统分析师和软件架构师 ...

  8. log4j2使用介绍

    工作中,用到了log4j2,以前只接触过log4j,也没有太过深入,这次就稍微系统的学习了以下log4j2. 一.引入pom.xml 使用maven作为项目的构建环境,pom.xml使用slf4j,s ...

  9. topcoder srm 475 div1

    problem1 link 暴力枚举$r$只兔子的初始位置,然后模拟即可. problem2 link 假设刚生下来的兔子是1岁,那么能够生小兔子的兔子的年龄是至少3岁. 那么所有的兔子按照年龄可以分 ...

  10. topcoder srm 706 div1

    1.给定一个迷宫,点号表示不可行,井号表示可行.现在可以改变其中的一些井号的位置.问最少改变多少个井号可以使得从左上角到右下角存在路径. 思路:设高为$n$,宽为$m$,若井号的个数$S$小于$n+m ...