Home > Mobile >  Image uploaded to the folder in CodeIgniter, but not in database
Image uploaded to the folder in CodeIgniter, but not in database

Time:04-05

I have a problem with my CodeIgniter project. The picture I am uploading, is only added to the folder, but not in database. Column name in database is "user_image".

This is my Controller:

         public function upload() {
            if(!$this->session->userdata('logged_in')) {
                redirect('users/login');
            }
            
            $data['title'] = "file Upload";
                $config['upload_path']          = './uploads/';
                $config['allowed_types']        = 'gif|jpg|png';
                $config['max_size']             = 500;
                $this->load->library('upload', $config);
                $data['error'] = "";
                if ( ! $this->upload->do_upload('userfile'))
                {
                        if(isset($_FILES['userfile'])){
                        $data['error'] = $this->upload->display_errors();
                        }
                        $this->load->view('templates/header');
                        $this->load->view('users/upload', $data);
                        $this->load->view('templates/footer');
                }
                else
                {
                        $user_id = $this->session->userdata('id');

                        $uploaddata = $this->upload->data();

                        $filename = $uploaddata['file_name'];

                        $userdata = array(
                            'user_image' => $filename
                        );

                        $this->user_model->update($user_id, $userdata);
                        $this->session->set_flashdata('message', "Upload Succesfully");
                        redirect('dashboard/index');
                } 
}

My model:

    public function update($user_id, $userdata) {
       $this->db->where('id', $user_id);
       $this->db->update('users', $userdata);
    
}

And my view:

<?php echo form_open_multipart('users/upload'); ?>
    <div >
        <div >
            <h1 ><?php echo $title; ?></h1>
            <div >
                <input type="file" name="userfile"  >
                <?php echo $error; ?>
            </div>
<br>
            
            <br>
            <div >
                <button type="submit" >Upload</button>
            </div>
        </div>
    </div>
    
<?php echo form_close(); ?>

Thank you very much in advance guys. I will appreciate any kind of help.

CodePudding user response:

You can try by returning a single item from array.

$filename = $this->upload->data('file_name'); // i.e. it will return test.jpg

Also check your database column type to accept / save filename as string

CodePudding user response:

Code above does not seem to have any obvious errors, but there are few items to check:

  • Related