2014-05-23 41 views
0

我正在使用Codeigniter上傳文件。當我符合文件要求時,它工作正常。問題是,如果有錯誤,我想顯示這些錯誤,但是當出現錯誤時,我的頁面只會刷新。這裏是我的代碼(我只得到了來自網絡的代碼並修改它):如何捕獲Codeigniter多個文件上傳錯誤?

foreach($_FILES['userfile'] as $key=>$val) 
{ 
    $i = 1; 
    foreach($val as $v) 
    { 
    $field_name = "file_".$i; 
    $_FILES[$field_name][$key] = $v; 
    $i++; 
    } 
} 
$err = 0; 
$i=0; 
// Unset the useless one ;) 
unset($_FILES['userfile']); 
$config['file_name'] = time(); 
$config['upload_path'] = './uploads/events'; 
$config['allowed_types'] = 'pdf'; 
$config['max_size'] = '200000'; 
$this->load->library('upload',$config); 
foreach($_FILES as $field_name => $file) 
{ 
    if ($this->upload->do_upload($field_name)) 
    { 
    $upload_data = $this->upload->data(); //UPLOAD FILES TO FOLDER 
    $this->Upload_items->files($upload_data['file_name']); //WRITE TO DATABASE; 
    }else{ 
    echo $this->upload->display_errors(); 
    } 
    $i++; 
    if(($i==$ctr)&&($err==0)){ 
    $this->Upload_items->event(); //WRITE TO MULTIMEDIA TABLE 
    } 
} 

功能寫入數據庫還沒有完成,順便說一句。

編輯:allowed_types的錯誤顯示,但超過文件大小的錯誤不是。

回答

0
$config ['upload_path'] = 'upload/Main_category_product/'; 
    $path = $config ['upload_path']; 
    $config ['allowed_types'] = 'gif|jpg|jpeg|png'; 
    $config ['max_size'] = '1024'; 
    $config ['max_width'] = '1920'; 
    $config ['max_height'] = '1280'; 
    $this->load->library ('upload', $config); 

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

     if (! empty ($value ['tmp_name']) && $value ['size'] > 0) { 

      if (! $this->upload->do_upload ($key)) { 

       $errors = $this->upload->display_errors(); 
       flashMsg ($errors); 
      } else { 
       // Code After Files Upload Success GOES HERE 
      } 
     } 
    } 

使用FlashMSG函數將消息正確地發送給用戶。或者收集數組中的所有消息並最終顯示。