Php CodeIgniter文件上载-Fat模型皮肤控制器
我想问一个问题 在我的模型中有一个方法,可以同时保存文件上传和图像处理。代码运行良好,但唯一的问题是我无法弄清楚如何将上传错误返回到控制器中,以便将其传递给视图并显示给用户 这是我的模型中的代码:Php CodeIgniter文件上载-Fat模型皮肤控制器,php,codeigniter,file-upload,Php,Codeigniter,File Upload,我想问一个问题 在我的模型中有一个方法,可以同时保存文件上传和图像处理。代码运行良好,但唯一的问题是我无法弄清楚如何将上传错误返回到控制器中,以便将其传递给视图并显示给用户 这是我的模型中的代码: class Foo_Model extends CI_Model { public function do_upload(){ $id = intval($this->input->post('id')); $config = array(
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;
}
}
}
我的控制器中的代码
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);
}
您是
return
来自Model
方法的错误,因此将返回的数据保存在变量中,并检查是否为error
或file\u 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);
}
嗨,女孩,谢谢你的回复!我还没有找到时间测试你的代码,但我会尽快告诉你。不过我有个问题。。基本上,上传和调整大小的$configs包含了业务逻辑,对吗?那么,我是否只需要在模型内部推送$configs,如果是这样,如何使选项动态化?