doumangzhen7204 2014-12-19 12:46
浏览 53
已采纳

CodeIgniter文件上传 - Fat模型皮肤控制器

I'd like to ask a question please.

I have inside my model a method that holds both file uploading and image manipulation. The code works fine, but the only problem is I can't figure out how to get the upload error back in the controller in order to pass it to the view and display it to the user.

This is the code in my model:

class Foo_Model extends CI_Model
{

    public function do_upload(){
        $id = intval($this->input->post('id'));
        $config = array(
            'upload_path' => './uploads/files/',
            'allowed_types' =>  'gif|jpg|png',
            'max_size'      =>  '2048',
            'max_width'     =>  '800',
            'max_heigth'    =>  '300',
            'overwrite'     =>  true,
            'file_name'     =>  'file_'.$id, // e.g. file_10.jpg
        );
        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload('file') ) {

            return $this->upload->display_errors();
        } else {
            // file uploaded successfully
            // now lets create some thumbs
            $upload_file = $this->upload->data();
            if ($upload_file['is_image']) {
                $config['image_library'] = 'gd2';
                $config['source_image'] = $upload_file['file_name'];
                $config['create_thumb'] = TRUE;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 75;
                $config['height'] = 50;

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

                $this->image_lib->resize();
            }

            // uploading and resizing was done
            return $upload_file;
            // return true;
        }
    }
}

Code in my controller

public function upload(){

    $this->foo_model->do_upload();
    // need to get the upload error (if any occured) or the upload data
    // how can I get them back from function of the model?

    $this->load->view('form_upload', $data);
}
  • 写回答

1条回答 默认 最新

  • drxm5014 2014-12-19 12:53
    关注

    You are return errors from Model method so hold returned data in variable and check that is error or file_data

    public function upload(){
    
        $upload_data = $this->foo_model->do_upload();
        //upload data will be return in array format
        if(is_array($upload_data)){
          $data['upload_file_info'] = $upload_data
        }else{ /*error as string so if not array then always error*/
          $data['error']  = $upload_data;
        }
    
        $this->load->view('form_upload', $data);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?