mysql - Uploading images to database in codeigniter? -
i tried searching no success want upload max 5 images database along user form data.i have table user data of form posted saved along images uploaded[image upload attached user form] picture fields in database named pic1,pic2,pic3.. pic5 + email,password etc successfull in uploading image data database not images.
//controller
if ($this->form_validation->run()!=true) { $data['countrydrop'] = $this->country_states_cities->getcountries(); $this->load->view('header'); $this->load->view('register',$data); //display page $this->load->view('footer'); }else{ $form=array(); $form['first_name']=$this->input->post("first_name",true); $form['last_name']=$this->input->post("last_name",true); $form['dob']=date('y-m-d',strtotime($this->input->post("dob",true))); $form['email']=$this->input->post("email",true); $form['password']=sha1($this->input->post("password",true)); $form['phone']=$this->input->post("phone",true); $form['addline2']=$this->input->post("addressline2",true); $form['zip']=$this->input->post("zip",true); $result = $this->couch_reg->insert_new_user($form); //call model
//model
function insert_new_user($form) { $this->db->insert('tbl_usrs',$form); if ($this->db->affected_rows() > 0) { return true; } else { return false; } }
//view
<input type="file" name="uploadfile[]" accept = "image/*" multiple = "multiple" size="20" /> <br />
as can see model part short want collect images name in array form , send database.
you need save images in upload folder , save image name in database.
<html> <body> <form method="post" action="<?php echo site_url('my-controller/file_upload');?>" 'enctype'=>'multipart/form-data'> <label for="file">filename:</label> <input type="file" name="userfile[]" id="file" multiple> <input type="submit" value="upload"></form> </body> </html>
then create controller make funtion inside it:
$files = $_files; $cpt = count($_files['userfile']['name']); for($i=0; $i<$cpt; $i++) { $_files['userfile']['name']= $files['userfile']['name'][$i]; $_files['userfile']['type']= $files['userfile']['type'][$i]; $_files['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; $_files['userfile']['error']= $files['userfile']['error'][$i]; $_files['userfile']['size']= $files['userfile']['size'][$i]; $this->upload->initialize($this->set_upload_options()); $this->upload->do_upload(); $filename = $_files['userfile']['name']; $images[] = $filename;
hope you. more can try this: http://w3code.in/2015/09/upload-file-using-codeigniter/
Comments
Post a Comment