通过codeigniter中的两种输入类型存储多个图像

时间:2017-07-08 07:06:49

标签: codeigniter

  

我想通过两种输入类型存储多个图像,但是一个输入类型的帖子有存储而另一个输入的帖子值没有存储。

我的观点

<span class="btn btn-round" title="Upload Photo">                                                                    
<input type="file" name="photo[]" multiple="">                                                                  
</span>
<span class="btn btn-round" title="Upload Photo">                                                                    
<input type="file" name="doc[]" multiple="">                                                                  
</span>
  

我的控制器

             if (!empty($_FILES['photo']['name'])) {
            $filesCount = count($_FILES['photo']['name']);          
            for ($i = 0; $i < $filesCount; $i++) {
                $_FILES['userFile']['name'] = $_FILES['photo']['name'][$i];
                $_FILES['userFile']['type'] = $_FILES['photo']['type'][$i];
                $_FILES['userFile']['tmp_name'] = $_FILES['photo']
                           ['tmp_name'][$i];
                $_FILES['userFile']['error'] = $_FILES['photo']['error']
                                         [$i];
                $_FILES['userFile']['size'] = $_FILES['photo']['size'][$i];

                $uploadPath = 'assets/uploads/';
                $type = 'photo';
                $config['upload_path'] = $uploadPath;
                $config['allowed_types'] = 'gif|png|jpg|jpeg';

                $this->load->library('upload', $config);
                $this->upload->initialize($config);
                if ($this->upload->do_upload('userFile')) {
                    $getdata = $this->upload->data();
                    $uploadData[$i] = $getdata['file_name'];
                }
            }
        }
        if(!empty($_FILES['doc']['name'])){
            $filesCounts = count($_FILES['doc']['name']);echo $filesCounts;
            for ($j = 0; $j < $filesCounts; $j++) {
                $_FILES['userFile']['name'] = $_FILES['doc']['name'][$j];
                $_FILES['userFile']['type'] = $_FILES['doc']['type'][$j];
                $_FILES['userFile']['tmp_name'] = $_FILES['doc']['tmp_name']
                                                                  [$j];
                $_FILES['userFile']['error'] = $_FILES['doc']['error'][$j];
                $_FILES['userFile']['size'] = $_FILES['doc']['size'][$j];

                $uploadPaths = 'assets/uploads/';
                $type1 = 'document';
                $config['upload_path'] = $uploadPath;
                $config['allowed_types'] =                       
                   'gif|png|jpg|jpeg|pdf|xls|docx|xlsx';
                $this->load->library('upload', $config);
                $this->upload->initialize($config);
                if ($this->upload->do_upload('userFile')) {
                    $getdata = $this->upload->data();
                    $uploadDocument[$j] = $getdata['file_name'];
                }
            }
        }
              for ($r = 0; $r < count($uploadData); $r++) {
                $data3 = array('event_id' => $last_id, 'file_name' => 
              $uploadData[$r], 'file_path' => $uploadPath, 'content_type' => 
              $type);
              if ($data3['file_name'] != "") {
              $this->admin_model->insert_activity_photo($data3);
              }
              }
              for ($a = 0; $a < count($uploadDocument); $a++) {
            $data5 = array('event_id' => $last_id, 'file_name' => 
         $uploadDocument[$a], 'file_path' => $uploadPaths, 'content_type' => 
         $type1);
        if ($data5['file_name'] != "") {
            $this->admin_model->insert_activity_photo($data5);
        }
        }
  

我的模特

function insert_activity_photo($data3 = array()){
    $this->db->insert('t_attachment',$data3);
}
function insert_activity_photos($data5 = array()){
    $this->db->insert('t_attachment',$data5);
}
  

当我尝试从这两种输入类型存储多个图像时,只有第一个输入类型值发布了未发布的第二个输入类型值。

1 个答案:

答案 0 :(得分:1)

嗨,两个图像的起始位置都是0($ I = 0和$ j = 0)所以文件是替换你应该更改密钥请检查我的代码 $ uploadDocument [$ k]的

 if (!empty($_FILES['photo']['name'])) {
        $filesCount = count($_FILES['photo']['name']); 
         $k=0;         
        for ($i = 0; $i < $filesCount; $i++) {

            $_FILES['userFile']['name'] = $_FILES['photo']['name'][$i];
            $_FILES['userFile']['type'] = $_FILES['photo']['type'][$i];
            $_FILES['userFile']['tmp_name'] = $_FILES['photo']
                       ['tmp_name'][$i];
            $_FILES['userFile']['error'] = $_FILES['photo']['error']
                                     [$i];
            $_FILES['userFile']['size'] = $_FILES['photo']['size'][$i];

            $uploadPath = 'assets/uploads/';
            $type = 'photo';
            $config['upload_path'] = $uploadPath;
            $config['allowed_types'] = 'gif|png|jpg|jpeg';

            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if ($this->upload->do_upload('userFile')) {
                $getdata = $this->upload->data();
                $uploadData[$k] = $getdata['file_name'];
            }
          $k++;
        }
    }
    if(!empty($_FILES['doc']['name'])){
        $filesCounts = count($_FILES['doc']['name']);echo $filesCounts;
        for ($j = 0; $j < $filesCounts; $j++) {
            $_FILES['userFile']['name'] = $_FILES['doc']['name'][$j];
            $_FILES['userFile']['type'] = $_FILES['doc']['type'][$j];
            $_FILES['userFile']['tmp_name'] = $_FILES['doc']['tmp_name']
                                                              [$j];
            $_FILES['userFile']['error'] = $_FILES['doc']['error'][$j];
            $_FILES['userFile']['size'] = $_FILES['doc']['size'][$j];

            $uploadPaths = 'assets/uploads/';
            $type1 = 'document';
            $config['upload_path'] = $uploadPath;
            $config['allowed_types'] =                       
               'gif|png|jpg|jpeg|pdf|xls|docx|xlsx';
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if ($this->upload->do_upload('userFile')) {
                $getdata = $this->upload->data();
                $uploadDocument[$k] = $getdata['file_name'];
            }
           $k++;
        }
    }
          for ($r = 0; $r < count($uploadData); $r++) {
            $data3 = array('event_id' => $last_id, 'file_name' => 
          $uploadData[$r], 'file_path' => $uploadPath, 'content_type' => 
          $type);
          if ($data3['file_name'] != "") {
          $this->admin_model->insert_activity_photo($data3);
          }
          }
          for ($a = 0; $a < count($uploadDocument); $a++) {
              $data5 = array('event_id' => $last_id, 'file_name' => 
              $uploadDocument[$a], 'file_path' => $uploadPaths, 'content_type' => $type1);
             if ($data5['file_name'] != "") {
                 $this->admin_model->insert_activity_photo($data5);
             }
    }