当图片全部是新增的时候,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. python之字符串函数

    1.  endswith()  startswith() # 以什么什么结尾 # 以什么什么开始 test = "alex" v = test.endswith('ex') v = ...

  2. python之运算符与基本数据类型

    1.开发工具:IDE pycharm(推荐).eclipse 2.运算符 结果是值 算数运算 a = 10 * 10 赋值运算 a = a + 1    a+=1 结果是布尔值 比较运算 a = 1 ...

  3. Jquery 插件 图片验证码

    摘自:https://www.cnblogs.com/lusufei/p/7746465.html !(function(window, document) { var size = 5;//设置验证 ...

  4. GoldenGate实时投递数据到大数据平台(4)- ElasticSearch 2.x

    ES 2.x ES 2.x安装 下载elasticSearch 2.4.5, https://www.elastic.co/downloads/elasticsearch 解压下载后的压缩包,启动ES ...

  5. 爬虫之牛掰的scrapy框架

    一. Scrapy简介及安装 http://python.jobbole.com/86405/ Scrapy的详细介绍   1.简介   2.安装     1.window上安装:         先 ...

  6. 在Eclipse中创建Dynamic Web Project具有和MyEclipse中Web Project一样的目录结构

    1.在Eclipse中新建Dynamic Web Project 1.1.修改default output folder build\classes修改为:WebRoot\WEB-INF\classe ...

  7. Docker学习笔记之为容器配置网络

    0x00 概述 在互联网时代,网络已经成为绝大多数应用进行数据交换的主要通道,Docker 作为集群部署的利器,在网络支持上也下了许多功夫.功能丰富和强大,并不代表使用复杂,在 Docker 的封装下 ...

  8. fjwc2019 D1T1 全连(dp+树状数组)

    #178. 「2019冬令营提高组」全连 显然我们可以得出一个$O(n^2)$的dp方程 记$f(i)$为取到第$i$个音符时的最大分数,枚举下一个音符的位置$j$进行转移. 蓝后我们就可以用树状数组 ...

  9. 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况

    在app.config中的configuration节内添加子节Startup,详细如下: <?xml version="1.0"?><configuration ...

  10. 第三周作业HAproxy文件操作

    #coding:utf-8 #Author:Mr Zhi """ HAproxy配置文件操作: 1. 根据用户输入输出对应的backend下的server信息 2. 可添 ...