如何捕获Codeigniter多文件上传错误?

时间:2014-05-23 01:57:24

标签: php codeigniter file-upload

我正在使用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的错误,但超出文件大小的错误不是。

1 个答案:

答案 0 :(得分: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功能将消息发送给用户。或收集数组中的所有消息并最终显示。