【问题标题】:CodeIgniter Image Upload - can't get error message to showCodeIgniter 图片上传 - 无法显示错误消息
【发布时间】:2011-04-19 00:38:04
【问题描述】:

这是我的上传模型

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

        }
    }

当我尝试上传大于 2MB 的文件时,无法让错误消息直接回显到浏览器。

公平地说,CI 会忽略这个大文件,并在文件小于 2MB 时正确上传。

唯一的问题是我无法在前端显示错误消息给用户一些反馈。

有什么想法吗?

【问题讨论】:

    标签: php image codeigniter upload


    【解决方案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);
                    }
    }
    }
    

    试试这个!!

    【讨论】:

      【解决方案2】:

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

      更新:

      如果你在 else 块中取出你的函数调用,然后放入一个 exit('too big');你能看到错误吗?如果是这样,您如何传递呼叫可能存在问题。

      【讨论】:

      • yes 'max_size' =&gt; 2048, 将文件限制为小于 2MB - 但是当我注释掉该行时,我仍然没有收到错误消息
      • post_max_size 是在 php.ini 中定义的,而不是你的脚本。
      • 哦 - 无论如何,如果我增加 'max_size'可以 上传图片 > 2MB 所以我认为 php.ini 的配置不会阻止它
      猜你喜欢
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 2018-10-30
      • 2019-02-02
      • 2012-03-05
      • 2021-01-30
      相关资源
      最近更新 更多