FILE,id不一致

当图片全部是新增的时候,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不对应的问题。

posted @ 2019-03-19 16:32  TBHacker  阅读(235)  评论(0编辑  收藏  举报