Home » Php » php – CodeIgniter – How to show error message from do_upload in model

php – CodeIgniter – How to show error message from do_upload in model

Posted by: admin February 25, 2020 Leave a comment

Questions:

I use uploading class to upload pdf files, but when I try to upload a file other than pdf, an error message does not appear. I know that I can solve this problem if I use uploading class in the controller, what I want to ask is whether it is possible to display an error message using uploading class in the models

Here’s my model code :

public function save()
    {
        $post = $this->input->post();
        $this->nip = $post["nip"];
        $this->id_jenis_berkas = $post["id_jenis_berkas"];
        $this->id_jenis_pengajuan = $post["id_jenis_pengajuan"];
        $this->file_berkas = $this->_uploadfile();
        $this->tanggal_upload = time($post["tanggal_upload"]);
        $this->db->insert($this->_table, $this);
    }

private function _uploadfile()
    {
        $this->load->helper('inflector');
        $file_name = underscore($_FILES['file_var_name']['name']);
        $config['upload_path']          = './upload/berkas/';
        $config['allowed_types']        = 'pdf';
        $config['file_name']            = $file_name;
        $config['overwrite']            = true;
        $config['max_size']             = 5120; // 1MB
        // $config['max_width']            = 1024;
        // $config['max_height']           = 768;`
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('file_berkas')) {
            $error = array('error' => $this->upload->display_errors()); 
        } else {
            return $this->upload->data("file_name");
        }
    } 

In Controller :

public function addberkas()
    {
        $data['title'] = 'Data Berkas';
        $data['user'] = $this->db->get_where('tbl_user', ['nip' => $this->session->userdata('nip')])->row_array();
        $data['jberkas'] = $this->db->get('tbl_jenis_berkas')->result_array();
        $data['jpengajuan'] = $this->db->get('tbl_jenis_pengajuan')->result_array(); `

        $berkas = $this->berkas_model;
        $validation = $this->form_validation;
        $validation->set_rules($berkas->rules());

        if ($validation->run()) {
            $berkas->save();
            $this->session->set_flashdata('success', 'Berhasil disimpan');
            redirect('data/berkas');
        } else {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('templates/header', $data);
            $this->load->view('templates/sidebar', $data);
            $this->load->view('templates/topbar', $data);
            $this->load->view("data/t_berkas", $data);
            $this->load->view('templates/footer');
        }
    }
How to&Answers:

Where you want to display the error message? based on your controller, the error message do not passed into view. you just write into variable “$error”. you can change this variable become:

$data['error'] = array('error' => $this->upload->display_errors());