i have a working multiple upload (document and image) CODEIGNITER -
i have made working multiple upload in codeigniter i'm having problem on how gonna insert file names of files on 2 tables (documents , images table) these 2 tables have 2 same column name (id, name). there way disjunct or compart code uploading image , doc. because united them in 1 function.
here code. working.
view
<?php echo form_open_multipart('test'); ?> <label>images</label> <input type='file' multiple='multiple' name='userfile[]'> <label>documents</label> <input type='file' multiple='multiple' name='userfile[]'> <?php echo form_submit('submit', 'upload them files!') ?>
controller
function index() { if (isset($_post['submit'])) { $this->load->library('upload'); //$this->uploadfile($_files['userfile']); $files = $_files; $cpt = count($_files['userfile']['name']); for($i=0; $i<$cpt; $i++) { $filename = $_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(); $this->upload->data(); } } $this->load->view("test"); } private function set_upload_options() { // upload image , document options $config = array(); $config['upload_path'] = './upload_documents/'; $config['allowed_types'] = 'jpg|png|gif|jpeg|jpg|png|gif|jpeg|pdf|doc|docx|xls|xlsx'; $config['max_size'] = '0'; // 0 = no file size limit $config['max_width'] = '0'; $config['max_height'] = '0'; $config['overwrite'] = true; return $config; }
these codes working , able transfer files on desired path. wondering on making model, how gonna identify file type seen codes above passed name of file on variable "$filename". if use print_r($filename), you'll see file name , it's file extension. names 1 insert 2 tables accordingly type of file.
is there code codeigniter or php code use identify file type , pass model 2 function upload_image or upload_docu? please.
so here is..
controller
function index() { if (isset($_post['submit'])) { $this->load->library('upload'); //$this->uploadfile($_files['userfile']); $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(); $this->upload->data(); $ext = pathinfo($filename, pathinfo_extension); $img_ext_chk = array('jpg','png','gif','jpeg','jpg','png', 'gif', 'jpeg'); if (in_array($ext,$img_ext_chk)) { $this->asset->add_image($filename); } else { $this->asset->add_document($filename); } } } }
and model
public function add_image($filename) { $data = array ('images' => $filename); $this->db->insert('asset_images', $data); } public function add_document($filename) { $data = array ('documents' => $filename); $this->db->insert('asset_documents', $data); }
Comments
Post a Comment