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))
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度