How to upload image path and name to database - Codeigniter

codeigniter, insert, mysql, php, upload

Solution

You must pass this data to your insert as array `$this->upload->data()`

At your Controller you must set when validation is done

else
{
    $this->MODELNAME->insert_images($this->upload->data());
    $data = array('upload_data' => $this->upload->data());
    $this->load->view('members/header');
    $this->load->view('members/upload_success', $data);
    $this->load->view('members/footer');
}

And at your model:

function insert_images($image_data = array()){
      $data = array(
          'filename' => $image_data['file_name'],
          'fullpath' => $image_data['full_path']
      );
      $this->db->insert('bedrijfimages', $data);
}

You can check what information is inside this array at CI documentation page: Codeigniter upload library

Problem

I know that this question has been asked several times, but all the other question I found are different from what I want. I want to upload the filename and filepath to a table called 'factoryimages'. What's the best way of doing this? My controller function: ``` function do_upload() { $config['upload_path'] = './assets/uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '1000'; $config['max_width'] = ''; $config['max_height'] = ''; $config['overwrite'] = TRUE; $config['remove_spaces'] = TRUE; $this->load->library('upload', $config); if ( ! $this->upload->do_upload()) { $error = array('error' => $this->upload->display_errors()); $this->load->view('members/header'); $this->load->view('members/upload_form', $error); $this->load->view('members/footer'); } else { $data = array('upload_data' => $this->upload->data()); $this->load->view('members/header'); $this->load->view('members/upload_success', $data); $this->load->view('members/footer'); } } ``` I tried this in my model, but it didn't work: ``` function insert_images() { $insert_data = array( 'filename' => $image_data['file_name'], 'fullpath' => $image_data['full_path'] ); $this->db->insert('bedrijfimages', $insert_data); } ``` My view: ``` <?php echo $error;?> <br/> <?php echo form_open_multipart('upload/do_upload');?> <input type="file" name="userfile" size="20" multiple="true" /> <br /><br /> <input type="submit" value="upload" /> </form> ``` Edit: I get an server error but i can't see what it is. My controller function: ``` function do_upload() { $config['upload_path'] = './assets/uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '1000'; $config['max_width'] = ''; $config['max_height'] = ''; $config['overwrite'] = TRUE; $config['remove_spaces'] = TRUE; $this->load->library('upload', $config); if ( ! $this->upload->do_upload()) { $error = array('error' => $this->upload->display_errors()); $this->upload_model->insert_images($this->upload->data()); $this->load->view('members/header'); $this->load->view('members/upload_form', $error); $this->load->view('members/footer'); } else { $data = array('upload_data' => $this->upload->data()); $this->load->view('members/header'); $this->load->view('members/upload_success', $data); $this->load->view('members/footer'); } } ``` My model function: ``` function insert_images($data = array()) { $data = array( 'filename' => $image_data['file_name'], 'fullpath' => $image_data['full_path'] ); $this->db->insert('bedrijfimages', $data); } ``` What could be the problem?

Original source