1

I just learn to save image data to database, those are filename and path. The path is appear on database, but not with filename. Whats the problem?

This is the controller,

function do_upload() { $config['upload_path']='./uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $this->load->library('upload', $config); $this->upload_model->upload($config); if(!$this->upload->do_upload()){ $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); } else { $data = array('upload_data' => $this->upload->data()); $this->load->view('upload_success', $data); } } 

and the model

 function upload ($config) { $config = $this->upload->data(); $upload_data = array( 'path' => $config['full_path'], 'nama_foto' => $config['file_name'] ); $this->db->insert('tb_picture', $upload_data); } 

and the table enter image description here

what should i do?

thank you before.

2
  • Try doing a var_dump($config) to check what's actually in that array. Commented Sep 16, 2013 at 15:18
  • 1
    @Nana Puspita please read your code line by line in order, does your $config variable have property [full_path] ? well, I guess no because you insert it in to database before actual file upload happens. Commented Sep 16, 2013 at 15:25

1 Answer 1

1

before reading this please try it again yourself, or try any kind of viedotutorial that is on the web http://net.tutsplus.com/sessions/codeigniter-from-scratch/

controller function should look like this

function do_upload() { $config['upload_path']='./uploads/'; //needs to set uploads folder CHMOD 0644 $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $config['overwrite'] = FALSE; $config['remove_spaces'] = TRUE; $field_name = "userfile"; //name tag in our HTML form in case you want to change it $this->load->library('upload', $config); if ( ! $this->upload->do_upload($field_name)) //upload happens { $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); } else { //succesful upload get data from upload and use them with our model $upload_data = $this->upload->data(); $this->upload_model->upload($upload_data); } } 

model function

function upload ($data) { if (empty($data) || $data === FALSE) return FALSE; $insert_data = array( 'path' => $data['full_path'], 'nama_foto' => $data['file_name'] ); $this->db->insert('tb_picture', $insert_data); return $this->db->insert_id(); //returns last inserted ID } 

Please note that your model was totaly "wrong" you used upload function in it, try to pass data only to it so model can process it.

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

1 Comment

thanks for your answer.. It's totally true. and the link, really interested. thank you so much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.