【问题标题】:Codeigniter: Returning variable between functionsCodeigniter:在函数之间返回变量
【发布时间】:2017-10-10 22:45:30
【问题描述】:

这是我的问题,我的代码一切正常,只是想在用户上传不允许的大小/类型图像时显示错误消息,全部在控制器中,或者我需要创建一个模型来传递 $错误变量?:

public function upload_face1($file1, $id){
    $config['upload_path'] = '././assets/administrador/images/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_width'] = '470';
    $config['max_height'] = '276';

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

    if (!$this->upload->do_upload($file1)) {
        $error = $this->upload->display_errors()); // The $error Variable that don't display in my view, I can't return it to the function editMetatags() to load the view from there
        return $error;
    } else {
        $register = $this->mmetatags->catchFace1($id);
        unlink('././assets/admin/images/'.$register->face1);
        $file_info = $this->upload->data();
        $save = $file_info['file_name'];
        $this->mmetatags->editFace1($id, $save);
        return true;
    }
}

public function editMetatags($d){
    $param["cod_meta"] = $d;
    $param['title'] = $this->input->post("title");
    $param['description'] = $this->input->post("description");
    $param['keywords'] = $this->input->post("keywords");

    if (isset($_FILES['face1']) && $_FILES['face1']['name'] != ''){
        $file1 = $this->upload_face1('face1', $d); //The file is there, but It wasn't uploaded beacause don't have the allowed size or type.
    }

    $data['error'] = $error; // Here is the variable "returned"
    $data['mensaje'] = 'success :D';
    $data["metatags"] = $this->mmetatags->loadMetatags($d);
    $this->load->view('administrador/editmetatags', $data, $error);

}

还是谢谢。

【问题讨论】:

    标签: php codeigniter return-value


    【解决方案1】:

    将错误发送到视图的一种简单方法是像这样使用 CodeIgniter 的 Load::vars():

    $this->load->vars(array('upload_errors' => $this->upload->display_errors()));
    

    那么在你看来:

    if( isset($upload_errors) )
          echo $upload_errors;
    

    【讨论】:

      猜你喜欢
      • 2019-11-12
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 2016-01-03
      • 2016-06-20
      相关资源
      最近更新 更多