2017-03-26 60 views
0

我有一個文件上傳器。我通過數據進行覆蓋。這讓我將這些數據上傳: - Pict1.jpg - Pict12.jpg - Pict13.jpgCodeigniter - PHP數組返回單個數據

但是,當我試圖返回它,它只是顯示pict1.jpg。下面的代碼

控制器:

function send(){ 
     $mariupload = $this->upload_gambar($_FILES['gambar']); 
     if ($mariupload === FALSE) { 
      $upload = false; 
     } 

     if($upload = false){ 
      $error = array('error' => $this->upload->display_errors()); 
      $this->load->view('upload_form', $error); 
     } else { 
      $dataupload = $mariupload; 
      echo $dataupload; 
     } 

    private function upload_gambar($files){ 
     //config buat upload gambar 
     $config['upload_path'] = 'path to upload'; 
     $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
     $config['max_size']  = '1024'; 
     $config['overwrite'] = '0'; 
     $this->load->library('upload', $config); 
     $gambar = array(); 
     if (is_array($files['name']) || is_object($files['name'])){ 
      foreach ($files['name'] as $key => $gambar) { 
       $_FILES['gambar[]']['name']= $files['name'][$key]; 
       $_FILES['gambar[]']['type']= $files['type'][$key]; 
       $_FILES['gambar[]']['tmp_name']= $files['tmp_name'][$key]; 
       $_FILES['gambar[]']['error']= $files['error'][$key]; 
       $_FILES['gambar[]']['size']= $files['size'][$key]; 

       $fileName = $gambar; 

       $images[] = $fileName; 

       $config['file_name'] = $fileName; 

       $this->upload->initialize($config); 

       if ($this->upload->do_upload('gambar[]')) { 
        $dataupload = $this->upload->data('file_name'); 
        return $dataupload.'<br>'; 
       } else { 
        $mariupload = false; 
       } 
      } 
     } 
    } 

當我嘗試回聲$ dataupload在upload_gambar功能,它可以作爲一個數組。但它作爲單個數據返回發送()函數。任何想法?

+0

編輯了$ mariupload。但是當我將gambar []變成gambar時,它變成了NULL。我有一個5個單獨的上傳器,將在1次提交中合併爲1個數組 –

回答

0

我終於找到了解決辦法。感謝Facebook上的Jovi Wang(https://www.facebook.com/jovi.wang.96?fref=ufi)。這是乾淨的代碼:

function send(){ 
     $try = $this->upload_multiple($_FILES['gambar']); 
       if(!$try = false){ 
       $anu= $this->upload_multiple($_FILES['gambar']); 
       foreach($anu as $key => $anu2){ 
       echo $anu2.'<br>'; 
       } 
} 

public function upload_multiple($files) 
    { 
     // echo '<pre>'; 
     // print_r($files); 
     // echo '</pre>'; 

     $config['upload_path']  = 'path to upload'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size']   = 1024; 
     $config['overwrite']  = 0; 
     $config['encrypt_name'] = TRUE; 
     $this->load->library('upload'); 
     $this->upload->initialize($config); 

     $data = array(); 

     foreach($files['name'] as $key => $value) 
     { 
      if (! empty($files['name'][$key])) 
      { 
       $_FILES['gambar_single']['name'] = $files['name'][$key]; 
       $_FILES['gambar_single']['name'] = $files['name'][$key]; 
       $_FILES['gambar_single']['type'] = $files['type'][$key]; 
       $_FILES['gambar_single']['tmp_name'] = $files['tmp_name'][$key]; 
       $_FILES['gambar_single']['error'] = $files['error'][$key]; 
       $_FILES['gambar_single']['size'] = $files['size'][$key]; 

       if ($this->upload->do_upload('gambar_single')) 
       { 
        $data[] = $this->upload->data('file_name'); 
       } 
       else 
       { 
        $data[] = $this->upload->display_errors(); 
       } 
      } 
      else 
      { 
       $data[] = 'empty upload'; 
      } 
     } 

     return $data; 
    } 
0

因爲你只發送一個文件

$mariupload = $this->upload_gambar($_FILES['gambar']); 

改變這

$mariupload = $this->upload_gambar($_FILES); 

然後在upload_gambar處理它作爲的foreach

+0

未定義的索引:upload_gambar函數中的名稱 –

0

你有內部回報循環。由於函數不會繼續執行,返回將中斷循環。嘗試提供一個數組來代替。

更換

return $dataupload.'<br>'; 

隨着

$return[] = $dataupload; 

,並在功能追加的末尾:

return $return; 
+0

它在upload_gambar()上給出數組,但在send()中返回單個文件:( –