2016-07-10 170 views
1

晚上好,我在代碼中遇到了問題。我正在做一個帶有單獨字段的圖庫表單。多個圖像上傳Codeigniter

我總是以「無」輸出結束,因爲即使我在縮略圖[]字段中輸入了某些內容,它仍未得到我的輸入。

任何人都有一個想法,我如何能夠解決它,以及如何做到這一點。非常感謝你。

這是我在HTML代碼:

Main Image: 
<input type="file" name="file1" required/> 
Thumbnails: 
<input type="file" name="thumbnails[]" /> 
<input type="file" name="thumbnails[]" /> 
<input type="file" name="thumbnails[]" /> 

在我的控制器:

$config = array(
    'upload_path' => "./uploads/workplace/", 
    'allowed_types' => "jpg|png|jpeg", 
    'remove_spaces' => TRUE, 
    'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb) 
    'max_height' => "0", 
    'max_width' => "0" 
    ); 
    $this->load->library('upload', $config); 
    if($this->upload->do_upload('file1')) 
    { 
     $config = array(
     'upload_path' => "./uploads/workplace/", 
     'allowed_types' => "jpg|png|jpeg", 
     'remove_spaces' => TRUE, 
     'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb) 
     'max_height' => "0", 
     'max_width' => "0" 
    ); 
     $this->load->library('upload', $config);    
     if($this->upload->do_upload('thumbnails[]')) 
     { 
     echo "yea"; 
     } 
     else 
     { 
     echo "none"; 
     } 
    } 

回答

0

因爲它已被寫在related answer,上傳庫不管理多個文件上傳,但你可以使用數組名稱遍歷整個$ _FILES數組,併爲每個縮略圖上傳。

您的foreach $ _FILES有類似的東西:

foreach ($_FILES as $key => $value) { 
    $this->upload->initialize($config); 
    if (!$this->upload->do_upload($key)) { 
     //here be errors 
    } 
} 
+0

我需要用codeigniter來做,所以驗證會很容易驗證。 –

+0

你只需要通過$ _FILES,但是你使用上傳庫驗證每個文件,只需設置上傳配置,而不是'$ this-> load-> library('upload',$ config);'(你已經在你的代碼),你可以使用'$ this-> upload-> initialize($ config);'。如果有任何錯誤,則將它們添加到錯誤數組中,以便稍後檢查和顯示。 – cssBlaster21895

+0

我總是得到這個你沒有選擇一個文件上傳。 –

0
$config = array(
'upload_path' => "./uploads/workplace/", 
'allowed_types' => "jpg|png|jpeg", 
'remove_spaces' => TRUE, 
'max_size' => "2048000", // Can be set to particular file size , here it is     2 MB(2048 Kb) 
'max_height' => "0", 
'max_width' => "0" 
); 
$this->load->library('upload', $config); 

foreach ($_FILES as $key) { 

     if (!$this->upload->do_upload($key)) { 
      echo $this->image_lib->display_errors(); 
     }else 
     { 
      echo "<strong>Your Thumb image has been successfully Uploded..!!</strong><br><br>"; 
     } 
    } 
0

試試這個代碼。

 $count = count($_FILES['filename']['size']); 
     //echo "<pre>"; print_r($_FILES); 
     foreach($_FILES as $key=>$value) 
     for($s=0; $s<=$count-1; $s++) 
     { 
     $_FILES['userfile']['name']=$value['name'][$s]; 
     $_FILES['userfile']['type'] = $value['type'][$s]; 
     $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s]; 
     $_FILES['userfile']['error']  = $value['error'][$s]; 
     $_FILES['userfile']['size'] = $value['size'][$s]; 
     $config['upload_path'] = './acontrol/document/'; 
     $config['allowed_types'] = 'gif|jpg|png|pdf'; 
     $this->load->library('upload', $config); 
     $this->upload->initialize($config); 
     $this->upload->do_upload(); 
     $data = $this->upload->data(); 
     //echo "<pre>"; print_r($this->upload->data());//die; 
     $file_rename=str_replace(".","_",$data['file_name']); 
     $names='acontrol/document/'.$data['file_name']; 

     if($data['file_name']!=""){ 
      $data=array(
       'fol5_id'=>$this->input->post('fol5_id'), 
       'doc_file'=>$names, 
       'doc_size'=>$value['size'][$s], 
       'delete_status'=>"NO" 
      ); 
      //echo "<pre>"; print_r($data);die; 
      $this->model->insertData("dms_client_doc",$data); 
      } 
     } 
+0

添加一些解釋並回答這個答案如何幫助OP修復當前問題 –