2017-09-02 52 views
0

用我的代碼下面它可以上傳只是單個文件,但不能上傳多個文件,請告訴我我做錯了什麼。無法使用CodeIgniter 3上傳多個文件3

這是錯誤消息。

陣列([錯誤] => 您沒有選擇要上傳的文件。

更新我的問題 當我做

echo $i.":".$_FILES['file_upload']['name'][$i].'<br/>'; 

這裏是結果

0:主標題 - 標題 - bg.jpg 1:varun.jpg

但之後我改變了代碼

$_FILES['file_upload']['name'] = $_FILES['file_upload']['name'][$i]; 
echo $i.":".$_FILES['file_upload']['name'].'<br/>'; 

下面是結果:

0 :heading-title-bg.jpg 1:e

//控制器

public function addPhoto(){ 

      if (!empty($_FILES)): 
       $count = count($_FILES['file_upload']['name']); 
       for($i =0; $i<$count;$i++): 

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

        $config['upload_path']   = './uploads/employee/'; 
        $config['allowed_types']  = 'jpg|png'; 
        $config['max_size']    = 5000; 
        $config['max_width']   = 0; 
        $config['max_height']   = 0; 
        $config['overwrite']   = FALSE; 
        $config['remove_spaces']  = TRUE; 

        $this->load->library('upload', $config); 
        $this->upload->initialize($config); 


        if($this->upload->do_upload('file_upload')){ 

         $data = $this->upload->data(); 
         echo "<pre>"; 
         print_r($data); 
         echo "</pre>"; 

        }else{ 
         $error = array('error' => $this->upload->display_errors()); 
         print_r($error); 
        } 

       endfor; 

      endif; //$_FILE 

    } 

VIEW

<input type="file" name="file_upload[]" class="form-control-file" id="fileUpload" multiple> 
+0

爲什麼你也算'[ '名']'場 –

+0

如果你做什麼ü得到:'的var_dump($ _ FILES [ 'file_upload']);在'權;模具()函數的頂部? – Sletheren

+0

陣列(5){ [ 「名稱」] =>數組 (2){ [0] => 串(38) 「的屏幕截圖2560年8月28日在13.40.35.png」 [1] => string(38)「Screen Shot 2560-08-28 at 19.47.01。PNG」 } [ 「型」] => 陣列(2){ [0] => 串(9) 「圖像/ PNG」 [1] => 串(9) 「圖像/ PNG」 } [ 「tmp_name的值」] => 陣列(2){ [0] => 串(14) 「/ TMP/phpJnbSu4」 [1] => 串(14) 「/ TMP/phpUMFg8l」 } [ 「錯誤」] =>數組 (2){ [0] => INT(0) [1] => INT(0) } [ 「大小」] => 陣列(2){ [0] => INT(110397) [1] => INT(83195) } } – xDevelopers

回答

0

這是工作的代碼在我的項目

$data = array(); 
      $filesCount = count($_FILES['daily_records']['name']); 
      for($i = 0; $i < $filesCount; $i++){ 
      $_FILES['daily_record']['name'] = $time."-".$_FILES['daily_records']['name'][$i]; 
      $_FILES['daily_record']['type'] = $_FILES['daily_records']['type'][$i]; 
      $_FILES['daily_record']['tmp_name'] = $_FILES['daily_records']['tmp_name'][$i]; 
      $_FILES['daily_record']['error'] = $_FILES['daily_records']['error'][$i]; 
      $_FILES['daily_record']['size'] = $_FILES['daily_records']['size'][$i]; 
      $uploadPath = './assets/uploads/daily_records/daily_record'; 
      $config['upload_path'] = $uploadPath; 
      $config['allowed_types'] = 'gif|jpg|png|pdf|docx'; 
      //$config['max_size'] = '100'; 
      //$config['max_width'] = '1024'; 
      //$config['max_height'] = '768'; 

      $this->load->library('upload', $config); 
      $this->upload->initialize($config); 
      if($this->upload->do_upload('daily_record')){ 
      $fileData = $this->upload->data(); 
      $file_name = $fileData['file_name']; 
      echo $file_name; 
    } 
} 
0

我已經通過自己的固定。

這裏是我最終的代碼現在是我的工作。

   foreach ($_FILES as $key => $v) { 

        for ( $i = 0; $i < count($v['name']); $i++){ 
         $_FILES['file_upload']['name']  = $v['name'][$i]; 
         $_FILES['file_upload']['type']  = $v['type'][$i]; 
         $_FILES['file_upload']['tmp_name'] = $v['tmp_name'][$i]; 
         $_FILES['file_upload']['error']  = $v['error'][$i]; 
         $_FILES['file_upload']['size']  = $v['size'][$i]; 

         //echo $_FILES['file_upload']['name'] ; 

         if(! $this->upload->do_upload('file_upload')){ 
          $error = array('error' => $this->upload->display_errors()); 
          print_r($error); 

         }else{ 
          $data = $this->upload->data(); 
          echo "<pre>"; 
          print_r($data); 
          echo "</pre>"; 
         } 
        }      
       }