2016-07-10 23 views
1

Guten Abend Leute, ich habe ein Problem in meinem Code. Ich mache ein Galerieformular mit separaten Feldern.Mehrere Bilder in Array hochladen Codeigniter

Ich habe immer eine "none" -Ausgabe, weil es meine Eingaben nicht erhält, obwohl ich etwas in das Feld thumbnails [] eingegeben habe.

Jeder hat eine Idee, wie ich es beheben kann und wie es gemacht werden kann. Vielen Dank.

Hier ist mein Code in 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[]" /> 

In meinem Controller:

$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"; 
     } 
    } 

Antwort

0

Wie es in related answer geschrieben worden ist, wird die Upload-Bibliothek nicht Hochladen mehrerer Dateien verwalten, aber Sie kann das gesamte $ _FILES-Array mit dem Namen des Arrays durchlaufen und für jedes einzelne Thumbnail hochladen.

Sie foreach $ _FILES mit etwas wie folgt aus:

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

Ich muss es mit Codeigniter tun, damit die Validierung einfach validiert werden kann. –

+0

Sie gehen einfach durch $ _FILES, aber Sie validieren jede Datei mit upload lib, stellen Sie einfach die upload config und anstatt '$ this-> load-> library ('upload', $ config);' (Sie haben das bereits in Ihrem Code) verwenden Sie '$ this-> upload-> initialize ($ config);'. Und wenn Fehler vorhanden sind, fügen Sie sie dem Fehlerarray hinzu, damit sie später überprüft und angezeigt werden können. – cssBlaster21895

+0

Ich bekomme immer das Sie haben keine Datei zum Hochladen ausgewählt. –

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

diesen Code Versuchen.

 $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

Fügen Sie einige Erklärungen mit der Antwort hinzu, wie diese Antwort dem aktuellen Problem helfen kann –