CodeIgniter - 如何显示来自do_upload的错误消息

时间:2013-10-03 20:22:46

标签: php codeigniter

我想在重定向链接上显示内容插入的成功消息。这是我的控制器代码: -

public function do_upload($field) {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100000';
        $config['max_width'] = '3000';
        $config['max_height'] = '3000';

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload($field)) {
            $error = array('error' => $this->upload->display_errors());

            return $error;
        } else {

            $updata =$this->upload->data();
            $data = $updata['raw_name'].$updata['file_ext'];
            return $data;
        }


     }


public function index() {

 $this->load->model('blog');

        if(isset($_POST['post']) && (!empty($_POST['post']) || strlen($_FILES['inputUpProfile']['name']) > 0) ){
        if(strlen($_FILES['inputUpProfile']['name']) > 0) 
        {
        $pic = $this->do_upload('inputUpProfile');
        print_r($pic);
        if ($this->input->post('post') == ''){$type="image";} else {$type="image-with-text";}
        }

        else {$pic = ""; $type = "text"; }

            $result = $this->blog->addPost($_SESSION['user_id'], $type  , $this->input->post('post'),$pic);
        }
$this->template->build('home_view',$this->data);
}

当上传大于3000的图片时,它会显示此消息

Array ( [error] =>

The filetype you are attempting to upload is not allowed.
) 

如何在我的视图中阅读此错误?

=============================================== =

更新: -

错误信息如下: -

Array ( [error] =>

The filetype you are attempting to upload is not allowed.
)
A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: mysql/mysql_driver.php

Line Number: 552
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at E:\AppServ\www\cc\ccc\application\controllers\home.php:83)

Filename: core/Common.php

Line Number: 442
A Database Error Occurred

Error Number: 1054

Unknown column 'Array' in 'field list'

INSERT INTO `events` (`ev_user_id`, `ev_type`, `ev_text`, `ev_pic`, `ev_date`) VALUES (445, 'image', '', Array, '2013-10-03 23:51:47')

Filename: E:\AppServ\www\cc\ccc\system\database\DB_driver.php

Line Number: 330

3 个答案:

答案 0 :(得分:10)

如果您收到错误

Array ( [error] =>
   The filetype you are attempting to upload is not allowed.
)
你可以在控制器中

if (!$this->upload->do_upload($field)) {
    $error = array('error' => $this->upload->display_errors());
    $this->session->set_flashdata('error',$error['error']);
    redirect('your_function_which_loads_the_view','refresh');
}

并在视图页面中执行类似

的操作
if($this->session->flashdata('error')){echo $this->session->flashdata('error');}

如果您遇到任何问题,请告诉我。

答案 1 :(得分:1)

仅在您的视图中显示错误,请按以下步骤操作: 首先加载会话库,在配置文件夹aoutoload.php中完成

$autoload['libraries'] = array('session');

根据需要在控制器中更改此代码段

  if (!$this->upload->do_upload($field)) {
    $this->session->set_flashdata( 'error_msg', $this->upload->display_errors() );
    redirect( base_url( 'your view' ) );
  }

在您的视图文件中...

    <?php
    if ( $this->session->flashdata( 'error_msg' ) ) {
        echo $this->session->flashdata('error_msg');
    }
    ?>

答案 2 :(得分:0)

$pic = $this->do_upload('inputUpProfile');
$this->data['upload_error'] = $pic['error'];

然后在视图中:

echo $upload_error;