doukuiqian9911 2018-09-27 01:12
浏览 84

在codeigniter中上传批量图像

I am new to Codeigniter and i am trying to update multiple images using loop. But i am unable to do so because the foreach loop updates only 1 record if there are 3 records. Thanks in advance.

Controller.php

if(!empty($_FILES['product_image']['name'][0])) {
      $number_of_files = sizeof($_FILES['product_image']['tmp_name']);
      $files = $_FILES['product_image'];
      $config = array(
          'upload_path' => FCPATH.'uploads/product_images/',
          'allowed_types' => 'jpg|png|jpeg'
      );
      for($i=0;$i<$number_of_files;$i++) {
          $_FILES['product_image']['name'] = $files['name'][$i];
          $_FILES['product_image']['type'] = $files['type'][$i];
          $_FILES['product_image']['tmp_name'] = $files['tmp_name'][$i];
          $_FILES['product_image']['error'] = $files['error'][$i];
          $_FILES['product_image']['size'] = $files['size'][$i];

          $this->upload->initialize($config);
          if($this->upload->do_upload('product_image')) {
              $imgData = $this->upload->data();
          }
          $img[] = array(
            'Image' => $files['name'][$i],
            'SortOrder' => $_POST['sort_order'][$i]
          );
          //unlink("uploads/product_images/".$_POST['path']);
      }
      if($query = $this->M_Product->editProductImg($img,$prodId)) {
        //$error = 0;
        print_r($query);
      } else {
        //$error = 1;
      }
    }

Model.php

public function editProductImg($img,$prodId) {
  $checkExist = $this->fetchSingleImage($prodId);
  if(empty($checkExist)) {
    if($this->db->insert_batch('tbl_product_images',$img)) {
      return true;
    } else {
      return false;
    }
  } else {
    foreach($img as $key => $value) {  

        $this->db->query("update tbl_product_images SET Image='".$value['Image']."',SortOrder='".$value['SortOrder']."' WHERE ProductId='".$prodId."'");

        return $this->db->last_query();
    }
  }
}
  • 写回答

1条回答 默认 最新

  • douchenchepan6465 2018-09-27 05:04
    关注

    Please check if number of files calculated are correct or try using

    $number_of_files= count($_FILES);
    

    Also change your first ' if ' to

    if(!empty($_FILES))
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题