在CodeIgniter中上传图像不起作用

时间:2013-08-24 21:40:21

标签: php codeigniter

我查看上传图片,我输入类型文件,如:

<input type="file" name="ev_pic" size="20" />

和我的控制器一样:

if(strlen($_FILES['ev_pic']['name']) > 0) 
        {
        $pic = $this->do_upload('ev_pic');
        }

上传方法:

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

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

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

            return $error;
        } else {
            /*$data = array('upload_data' => $this->upload->data());
            return $data;*/
            $updata =$this->upload->data();
            $data = $updata['raw_name'].$updata['file_ext'];
            return $data;
        }
    }
在调试中它显示了这个错误:

Severity: Notice

Message: Undefined index: ev_pic

Filename: administrator/events.php

Line Number: 68


Error Number: 1054

Unknown column 'Array' in 'field list'

UPDATE `events` SET `ev_id` = 0, `ev_text` = 0, `ev_pic` = Array

Filename: D:\AppServ\www\d5n\rashaqa2\system\database\DB_driver.php

Line Number: 330

所以我会按ev_pic命名文件,问题出在哪里?

2 个答案:

答案 0 :(得分:1)

在控制器的__construct函数中加载上传库,如下所示:

public function __construct(){
  parent::__construct();
  $this->load->library('upload');
}

然后将您的功能更改为:

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

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

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

            return $error;
        } else {
            /*$data = array('upload_data' => $this->upload->data());
            return $data;*/
            $updata =$this->upload->data();
            $data = $updata['raw_name'].$updata['file_ext'];
            return $data;
        }
    }

答案 1 :(得分:0)

请检查您的表格

表单enctype =“multipart / form-data”是否可用

并更改此行

if(!$ this-&gt; upload-&gt; do_upload($ field)){to if(!$ this-&gt; upload-&gt; do_upload('ev_pic')){