CodeIgniter Image Upload - can't get error message to show

折月煮酒 提交于 2019-12-11 19:33:54

问题


This is my upload model

    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();

        }
    }

I can't get the error message to echo straight to the browser when I try uploading a file > 2MB.

To be fair, CI ignores this large file, and uploads correctly when a file is < 2MB.

The only thing is that I can't get the error message to show on the front-end to give the suer some feedback.

Any ideas what's wrong here?


回答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);
                }
}
}

try this!!




回答2:


Is your post_max_size limit less than 2MB? (http://ca3.php.net/manual/en/ini.core.php#ini.post-max-size) If so the file may have been discarded before your code is invoked.

Update:

If you take out your function call in the else block, and just drop in an exit('too big'); are you able to see errors then? If so there may be an issue with how you're pasing the call off.



来源:https://stackoverflow.com/questions/5710352/codeigniter-image-upload-cant-get-error-message-to-show

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!