1

I have the following html code

<form action="form-handler.php" method="post" enctype="multipart/form-data">
    <div>
            <input id="myfile" name="myfile" type="file">
            <input value="Upload ►" type="submit">
    </div>
</form>

I want to upload multiple files at once. What should I do in controller to upload the selected files in codeigniter ? I tried using the Codeigniter file upload library, but it is not loading multiple files. Though, for 1 file it is working fine.

tim peterson
  • 23,653
  • 59
  • 177
  • 299
Faizan Ali
  • 509
  • 12
  • 35
  • 1
    CI currently does not support multiple files. Possible duplicate: http://stackoverflow.com/questions/9276756/codeigniter-multiple-file-upload – Seabass May 07 '12 at 16:00
  • so you are saying that there is no way to upload multiple files ? – Faizan Ali May 07 '12 at 16:02
  • I am saying that by default, "CI currently does not support multiple files`. If you check the links on the side, you'll see how hundreds of others have found a way to edit the core to allow multiple files. – Seabass May 07 '12 at 16:03
  • Answered it here: http://stackoverflow.com/questions/9276756/codeigniter-multiple-file-upload/36943949#36943949 – Rauli Rajande Apr 29 '16 at 17:09

1 Answers1

3

You can just call Codigniter's do_upload() function within a foreach loop that iterates through your array of posted files, $_FILE;

here's the code for this that goes in the controller function which receives the upload form POST:

$this->load->library('upload');
$this->upload->initialize($config); //$config is array like as [in CI's documentation][1] 
$path='uploads/some_folder'; // refers to the root's uploads/some_folder folder
$upload_data = $this->upload->multiFileUpload($path, TRUE);

The magic is in the multiFileUpload() function which I got from someone else. This function is extended on CI's Upload library by putting it in a file called MY_Upload.php in my /application/libraries folder.

Here's the entire contents of MY_Upload.php which is really just the multiFileUpload() function so don't be intimidated you don't need to know anything other than the $path of where you want the files to go.

<?

class MY_Upload extends CI_Upload {


public function multiFileUpload($path, $protect = FALSE){

  /*
  * Declare uploaded_info and uploaded_files
  * when i'm sure $_FILES has some data
  */
 /* if($this->upload_path[strlen($this->upload_path)-1] != '/')
   $this->upload_path .= '/';*/

   //$this->upload_path=$path;

  if(isset($_FILES)){

   #Here we check if the path exists if not then create
   /*if(!file_exists($this->upload_path)){
    @mkdir($this->upload_path,0700,TRUE);
   }*/

    if(!file_exists($path)){
    @mkdir($path,0700,TRUE);
   }  
    $uploaded_info  = FALSE;
    /*
    * The structure of $_FILES changes a lot with the array name on the input file,
    * then i'm gonna modify $_FILES to make it think the data comes from several
    * input file instead of one "arrayfied" input.
    *
    * The several ways to upload files are controled with this if...else structure
    */
    if(count($_FILES) == 1)
    {
        $main_key = key($_FILES);
        if(is_array($_FILES[$main_key]['name']))
        {

            foreach($_FILES[$main_key] as $key => $value)
            {                

                for($y = 0; $y < count($value); $y++)
                {

                    $_FILES[$main_key .'-'. $y][$key] = $value[$y];

                }


            }

            unset($_FILES[$main_key]);

            $uploaded_files  = $_FILES;
        }
        else
        {
            $uploaded_files  = $_FILES;    
        }

    }
    else
    {
        $uploaded_files  = $_FILES;    
    }

   #Here we create the index file in each path's directory
   /*if($protect){
    $folder = '';
    foreach(explode('/',$this->upload_path)  as $f){

     $folder .= $f.'/';
     $text = "<?php echo 'Directory access is forbidden.'; ?>";

     if(!file_exists($folder.'index.php')){
      $index = $folder.'index.php'; 
      $Handle = fopen($index, 'w');
      fwrite($Handle, trim($text));
      fclose($Handle); 
     }
    }   
   }*/

   #Here we do the upload process

   foreach($uploaded_files as $file => $value){
    if (!$this->do_upload($file))
    {
     $uploaded_info['error'][]  =  array_merge($this->data(),
              array('error_msg' => $this->display_errors()));

    }
    else
    {
     $uploaded_info['upload_data'][] =  array_merge($this->data(),
              array('error_msg' => $this->display_errors()));
    }
   }  
  }

  #Then return what happened with the files
  return $uploaded_info;
 } 
}
tim peterson
  • 23,653
  • 59
  • 177
  • 299