CodeIgniter图像上传 - 无法显示错误消息

时间:2011-04-19 00:38:04

标签: php image codeigniter upload

这是我的上传模式

    function upload_avatar()
    {
        $id = $this->tank_auth->get_user_id();

        //config upload parameters and upload image
        $config = array(
            'allowed_types' => 'jpeg|jpg|png',
            'upload_path' => $this->upload_path,
            'max_size' => 2048,
            'encrypt_name' => TRUE,
            'overwrite' => FALSE,
        );
        $this->load->library('upload', $config);
        $this->upload->do_upload();

        //get upload data, config, resize uploaded image, save in avatars subfolder
        $image_data = $this->upload->data();

        if ($image_data['file_size'] < 2048) {

            $config = array(
                'source_image' => $image_data['full_path'],
                'new_image' => $this->upload_path . '/avatars',
                'maintain_ratio' => TRUE,
                'width' => 125,
                'height' => 125
            );
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();

            //only burn avatar path to user_profiles table if no upload errors
            if (!$this->upload->display_errors()) {
                $data = array('avatar' => base_url() . 'images/avatars/' . $image_data['file_name']);
                $this->db->where('id', $id);
                $this->db->update('user_profiles', $data);
            }

            //delete the original file from server
            $this->load->helper('file');
            unlink($image_data['full_path']);

        } else {

    echo $this->upload->display_errors();

        }
    }

当我尝试上传文件时,我无法收到错误消息直接回显到浏览器&gt; 2MB。

公平地说,CI会忽略这个大文件,并在文件&lt; 2MB。

唯一的问题是,我无法在前端显示错误消息,以便为其提供一些反馈。

这里有什么想法吗?

2 个答案:

答案 0 :(得分:1)

$config['upload_path'] = 'uploads/category/'.$id.'/';
        //echo $file_name;die;
        //echo $config['upload_path'];
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '2048';
        $config['max_width'] = '1920';
        $config['max_height'] = '1280';
        $this->load->library('upload');
         foreach ($_FILES as $key => $value) {
            //print_r($key);

            if (!empty($key['name'])) {

                $this->upload->initialize($config);
                if (!$this->upload->do_upload($key)) {
                  // echo 'test';die;
//                    rmdir('uploads/category/'.$id);
                    $errors = $this->upload->display_errors();
                    flashMsg($errors);
                }
}
}

试试这个!!

答案 1 :(得分:0)

您的post_max_size限制是否小于2MB? (http://ca3.php.net/manual/en/ini.core.php#ini.post-max-size)如果是这样的话,在调用代码之前可能已经丢弃了该文件。

更新:

如果你在else块中取出你的函数调用,只需要退出('太大');你能看到错误吗?如果是这样,可能会出现如何解决这个问题的问题。