2015-06-09 175 views
1

我是CI 3的初學者,我想上傳兩個文件圖像。我試過谷歌,但沒有任何作品。 感謝您的幫助CodeIgniter 3 - 多文件上傳

HTML查看

<form method="post" accept-charset="utf-8" action="Kasprof" enctype="multipart/form-data"> 
     <div class="form-group"> 
      Parent/Potvrdenie zákonného zástupcu 
      <input name="images[parent]" type="file"> 
     </div> 
     <div class="form-group"> 
      Doctor/Potvrdenie od doktora 
      <input name="images[doctor]" type="file"> 
     </div>    
     <button type="submit" class="btn btn-default">Send/Poslať</button> 
</form> 

PHP控制器:

$config['upload_path'] = './uploads/'; 
$config['allowed_types'] = 'gif|jpg|png|pdf|jpeg'; 
$config['max_size'] = "4096000"; 
$config['max_width'] = "4096"; 
$config['max_height'] = "4096"; 
$this->upload->initialize($config); 

$this->upload->do_upload($_FILES); 

錯誤:

Message: Illegal offset type in isset or empty 
Filename: libraries/Upload.php 
Line Number: 377 

Message: preg_match_all() expects parameter 2 to be string, array given 
Filename: libraries/Upload.php 
Line Number: 382 
+0

任何錯誤輸出? – Tool

+0

@Tool我忘了對不起。 –

回答

1

通過上傳文件循環應該工作。

foreach($_FILES as $userfile){ 
    //some code here 
} 
1

您可以使用此功能上傳多個上傳文件。 其中$userfile是輸入文件名,$image_path是您的目標路徑,$allowed允許的類型,$max_size允許的最大上傳大小。

function _multi_upload_files($userfile,$image_path,$allowed,$max_size) 
{ 
    $this->ci->load->library('upload'); 
    if(!is_dir($image_path)) 
    { 
     mkdir($image_path); 
    } 
    $files = $_FILES; 
    $cpt = count($_FILES[$userfile]['name']); 
    for($i=0; $i<$cpt; $i++) 
    { 
     if($files[$userfile]['tmp_name'][$i]!='') 
     { 
      $_FILES[$userfile]['name']= $files[$userfile]['name'][$i]; 
      $_FILES[$userfile]['type']= $files[$userfile]['type'][$i]; 
      $_FILES[$userfile]['tmp_name']= $files[$userfile]['tmp_name'][$i]; 
      $_FILES[$userfile]['error']= $files[$userfile]['error'][$i]; 
      $_FILES[$userfile]['size']= $files[$userfile]['size'][$i];  

      $config['upload_path'] = $image_path; 
      $config['allowed_types'] = $allowed; 
      $config['max_size'] = $max_size; 
      // if want to rename file 
      $img=$_FILES[$userfile]['name'][$i]; 
      $random_digit=rand(00,99999); 
      $ext = strtolower(substr($img, strpos($img,'.'), strlen($img)-1)); 
      $file_name=$random_digit.$ext; 
      $config['file_name'] = $file_name; 
      // end renaming 
      $this->ci->upload->initialize($config); 
      $this->ci->upload->do_upload($userfile); 
      $newfile[]=$this->ci->upload->file_name; 
     } 
    } 

    return $newfile; 
}