在codeigniter中使用上传类 - 模型还是控制器?

dav*_*019 2 upload codeigniter class file

关于CI的快速问题.

我有一个表单,几个文本输入字段和一个文件上传的视图.我希望能够从文本字段中获取输入,将其保存到数据库,然后上传图像.

我通过在控制器中使用上传代码实现了这一点,如果上传成功,则调用我的模型来更新数据库.

这是"最佳实践",还是一种可接受的做法?或者文件上传应该在模型中.有关系吗?

基本上我的代码是:

    function edit_category()
        {
            $config['upload_path'] = 'images/category/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '1000';
            $config['max_width'] = '300';
            $config['max_height'] = '300';

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

            if(!$this->upload->do_upload()) 
            {
                $this->session->set_flashdata('status', $this->upload->display_errors());
                        redirect('admin/category/edit/'.$this->input->post('catID'), 'location');
            }
            else  /*no errors, upload is successful..*/
            {
                $fInfo = $this->upload->data();
                //$this->_createThumbnail($fInfo['file_name']);
                            //process form POST data.
                            $data = array(
                                'catName' => $this->input->post('catName'),
                                'catDesc' => $this->input->post('catDesc'),
                                'catImage' => $fInfo['file_name']
                );

/* update the database */
                $category = $this->category_model->edit_category($data, $this->input->post('catID'));
Run Code Online (Sandbox Code Playgroud)

mus*_*c80 5

我会把它放在一个模型中,因为我喜欢让我的控制器尽可能地保持苗条.我认为控制器是视图和后台处理之间的链接,而不是处理本身.我不确定这是否是"最佳实践".它肯定会以你的方式运作.CodeIgniter允许您在应用mvc理论方面非常灵活.