How to Upload multiple files using codeigniter

View: 1068    Dowload: 0   Comment: 0   Post by: hanhga   Category: Codeigniter   Fields: Other

CodeIgniter is a PHP framework, CodeIgniter has a number of helpers and libraries .

which will reduce the development time and we can write more reliable and bugs free Code. Here ,This post is about Upload multiple files using codeigniter, CodeIgniter has upload library , by using this Class we can upload files on server very easily.

For Uploading a files, as usually we need a Simple HTML from, with a input field and submit button.

Uploading multiple files using codeigniter is very easy.

First set the file name to userfile[](we need to send the file name in array).We need to send the multiple file name in array.Do not forget to set ‘enctype’=>’multipart/form-data’.

<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>
  class My_Controller extends CI_Controller {
  public function __construct() {
 public function file_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];
                $fileName = $_FILES['userfile']['name'];
                 $images[] = $fileName;
  $fileName = implode(',',$images);
private function set_upload_options()
  // upload an image options
         $config = array();
         $config['upload_path'] = './upload/'; //give the path to upload the image in folder
         $config['allowed_types'] = 'gif|jpg|png';
          $config['max_size'] = '0';
         $config['overwrite'] = FALSE;
  return $config;

In Model:

class My_Model extends CI_Model {
public function upload_image($fileName)
if($filename!='' ){
      $filename1 = explode(',',$filename);
  foreach($filename1 as $file){
  $file_data = array(
  'name' => $file,
  'datetime' => date('Y-m-d h:i:s')
  $this->db->insert('uploaded_files', $file_data);

Hope this article will help you.

How to Upload multiple files using codeigniter

CodeIgniter is a PHP framework, CodeIgniter has a number of helpers and libraries .

Posted on 08-01-2016 


To comment you must be logged in members.

Files with category

File suggestion for you
File top downloads
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd