1

I want to upload the image to database using CodeIgniter. It saved to database but the image didn't send to the folder path I've set and only the image_path are saved to the database. Can you tell me what to do or anything I did wrong?

Here's my code:

*** Controller ***


function saveEvent(){
        
        $config = [
            'upload_path' => './assets/img',
            'allowed_types' => 'gif|png|jpg|jppeg'
        ];

        $this->load->library('upload', $config);
        $this->form_validation->set_error_delimiters();
        $this->upload->do_upload();
        // var_dump('asd'. $this->upload->do_upload());
        // if($this->upload->do_upload()){
            $datas = $this->input->post('eventimage');
            $info = $this->upload->data();
            $image_path = "./assets/img/".$info['raw_name'].$info['file_ext'];
            $data['cal_events_image'] = $image_path;
            $data['cal_events_name'] = $this->input->post('eventname');
            unset($data['submit']);
            $this->load->model('CalendarModel');
            if($this->CalendarModel->eventinsertdata($data)){
                echo "success";
            }else{
                echo " fail";
            }

*** View ***


<form action="<?php echo base_url('index.php/CalendarController/saveEvent');?>" enctype="multipart/form-data" method="post" accept-charset="utf-8">
                <table class="table">
                    <tr>
                        <td>Event Type</td>
                        <td><input type="text" name="eventname"></td> 
                    </tr>
                    <tr>
                        <td>Image</td>
                        <td><input type="file" name="eventimage"></td>
                    </tr>
                    <tr>
                        <td colspan="2"><input type="submit" name="submitevent" value="Add Event" class="btn btn-success"></td>
                    </tr>
                </table>
            </form>

*** Model ***

function eventinsertdata($data){
return $this->db->insert('table', $data);
     return $this->db->insert('dev_adkt_events_type', $data);
        echo 'hahay';

    }

What can I try to resolve this?

2
  • 2
    First, try this code to check the error if($this->upload->do_upload()){ //Coding...}else{ print_r($this->upload->display_errors());}. ELSE part to get error Commented Apr 12, 2019 at 7:11
  • @DanishAli hello sir i will update my code and change a little bit my question. But thank you sir. Commented Apr 12, 2019 at 7:20

1 Answer 1

1

I already solve it! here is my answer and it works!

Controller


function saveEvent(){
        $config = array(
            'upload_path' => './assets/img',
            'allowed_types' => 'gif|png|jpg|jppeg'
        );

        get_instance()->load->library('upload', $this->config); 
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        $this->form_validation->set_error_delimiters();
        if($this->upload->do_upload('eventimage')){
            $data = $this->input->post('eventimage');
            $info = $this->upload->data();
            // var_dump($info);
            $image_path = "/assets/img/".$info['raw_name'].$info['file_ext'];
            $datas['cal_events_image'] = $image_path;
            $datas['cal_events_name'] = $this->input->post('eventname');
            unset($data['submit']);
            $this->load->model('CalendarModel');
            if($this->CalendarModel->eventinsertdata($datas)){
                // echo "success";
                redirect($_SERVER['HTTP_REFERER']);
            }else{
                echo $this->upload->display_errors(); 
            }
        }else{
            // echo 'shit';
            echo $this->upload->display_errors(); 
        }
    }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.