multiple files upload in codeigniter
Multiple files upload in codeigniter
BW Creative is known as the best web development company in Panipat. We are backed with a team of expert PHP Developer, SEO Consultants, Graphic Designer from Delhi – NCR & Chandigarh. Today, We are sharing how to allow multiple files upload in codeigniter, a powerful PHP framework.
This code will allow multiple files upload in codeigniter Framework with Javascript based file validation.
Create a new controller or add the following code to your existing controller.
public function do_upload() { $this->load->library('upload'); $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(); } $data['page_name'] = 'imgupload'; // change this to your view name $data['page_title'] = 'Bulk Uploader'; //change this to your page title $this->load->view('backend/admin/index', $data); //change this accordingly to your application } private function set_upload_options() { // upload an image options $config = array(); $config['upload_path'] = 'uploads/demo'; //change this to your uploading path $config['allowed_types'] = 'jpg'; //change this to your allowed file types with | seperator $config['max_size'] = '0'; //change this to your max file size upload $config['overwrite'] = FALSE; // change this to TRUE if you want to allow overwrite return $config; }
Add the following code to your view
<?php echo form_open_multipart('admin/do_upload');?> //change this to your controller name <input id="buf" type="file" required multiple name="userfile[]" size="20" onchange="CheckFileName()" /> <input type="submit" value="upload" />
We have also used javascript validation for above script, Here is the JS Code
function CheckFileName() { var fileName = document.getElementById("buf").value if (fileName.split(".")[1].toUpperCase() == "JPG") return true; else { alert("File with " + fileName.split(".")[1] + " is invalid. Upload a valid file with JPG extensions"); document.getElementById("buf").value = ""; } return true; }