繁体   English   中英

在Codeigniter中批量上传图像

[英]Batch image upload in codeigniter

我是Codeigniter的新手,我正在尝试使用loop更新多个图像。 但是我无法这样做,因为如果有3条记录,则 foreach loop 仅更新1条记录 提前致谢。

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();
    }
  }
}

请检查计算出的文件数是否正确或尝试使用

$number_of_files= count($_FILES);

同时将您的第一个'if'更改为

if(!empty($_FILES))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM