Upload multiple files using codeigniter

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’.


  1. <form method="POST" action="<?php echo site_url('my-controller/file_upload');?>" enctype='multipart/form-data'>
  2. <label for="file">Filename:</label>
  3. <input type="file" name="userfile[]" id="file" multiple>
  4. <input type="submit" value="upload"></form>
  5. </body>
  6. </html>


Then create controller and load the library. This code will work for both single upload image and multiple image upload.

  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.


  • Pingback: How to upload file using ajax in codeigniter()

  • Deepak Prasad

    How to check file is exist or not.. When i upload files

    • Ricky Negi

      I will suggest you to rename the file name instead of checking for duplicate file name…

      • sarfraaz talat

        check my comment buddy, i think it has solution for your problem

  • Elinardo Silva

    friend which tables should I create in my database? sorry for my english. i’m from brazil. thanks a lot!

  • sarfraaz talat

    There is a little bug in this code .
    Inside Mycontroller.php you set the filename by writing $fileName = $_FILES['userfile']['name'] which gets the actual file name before its uploaded if its name is changed because of duplication the $filename will have that old name. so it will create problem of duplication in database. i suggest & highly recommend you to use

    $upload_data = $this->upload->data();
    $fileName = $_FILES['userfile']['name'];

    to get the fileName . this will get you the correct filename even if its changed while uploading.

  • Special Funn

    not working

  • Special Funn

    this code has many problems .. 😛

  • Vik Dhr

    hey i have modify your model function just rename the $filename with $fileName

    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);




  • http://www.bibinjohn.byethost7.com BIBIN JOHN

    This is nice, usefull for me… thanks

  • http://animeichi.com/ Ahmad Husen

    Hey, I found another bug in your code.
    Please see MyController, and replace:



    Sorry, for my bad english. 😀


  • jalpesh patel

    hello w3code your logic is awesome but some mistakes are there plz correct it:
    in view : site_url(‘my-controller) here my_controller i.e. _ not –
    in model : public function upload_image($fileName) here change $filename not ‘N’ but ‘n’ otherwise on model file other code doesn’t work
    in model : And main thing how can you load view .. ?????
    no code written here for that so add below function :
    public function load_view(){

    now in url write :
    http://localhost/****/my_controller/load_view here *** means your project name