How to Upload multiple files in codeigniter?

Do you feel trouble in Uploading multiple files in Codeigniter??

we are here to help you. We can easily upload multiple files in Codeigniter. You have to follow these steps for doing same:-

Step1: Firstly you have to download fresh new version of Codeigniter. Here is the link for download codeigniter.

Download

Step 2: Routing will decide whether to pass the request, so we have to create Route for our applitaion. Go to application>>config>>routes.php file and paste the code shown in below :

$route[‘multiple-upload’] = ‘MultipleUpload’;

Step 3: In the next step we have to create “MultipleUpload” Controller with index( ) and imageupload( ). So Go to

application>> controller>> MultipleUpload.php

Paste below code in your MultipleUpload.php file;-

<?php
  
class MultipleUpload extends CI_Controller {

   public function __construct() { 
      parent::__construct(); 
      $this->load->helper('url'); 
        $this->load->helper('form');
$this->load->library('form_validation');
$this->load->library('upload');
   }
  
   public function index() { 
      $this->load->view('multipleUploadForm'); 
   } 
    

function Imageupload(){

    $this->load->library('upload');

    $files = $_FILES;
    $count = count($_FILES['files']['name']);
  for($i=0; $i<$count; $i++){
        $_FILES['files']['name']   = $files['files']['name'][$i];
        $_FILES['files']['type']   = $files['files']['type'][$i];
        $_FILES['files']['tmp_name'] = $files['files']['tmp_name'][$i];
        $_FILES['files']['error']  = $files['files']['error'][$i];
        $_FILES['files']['size']   = $files['files']['size'][$i];    

      $this->upload->initialize($this->set_upload_options());
      $this->upload->do_upload();

      $upload_data  = $this->upload->data();
        $file_name  =   $upload_data['file_name'];
        $file_type  =   $upload_data['file_type'];
        $file_size  =   $upload_data['file_size'];

      // Output control
      $data['getfiledata_file_name'] = $file_name;
      $data['getfiledata_file_type'] = $file_type;
      $data['getfiledata_file_size'] = $file_size;

       $uploadData = $this->upload->data();
            $filename = $uploadData['file_name'];
   
            $data['totalFiles'][] = $filename;
        //Create a view containing just the text "Uploaded successfully"
    $this->load->view('upload_success', $data);

  }

}
private function set_upload_options(){   
  //  upload an image options
    $config = array();
    $config['upload_path'] = base_url().'Media';
    $config['allowed_types'] = 'gif|jpg|png|pdf';
    $config['max_size']      = '0';
    $config['overwrite']     = FALSE;


    return $config;
}
} 
?> 

Step 4: Create Media folder in your Project Folder to save your uploaded images.

Step 5: Now we have to create view file to show our form to upload images. Create “multipleUploadForm.php” file in views folder.So go to application>>views>>multipleUploadForm.php

Paste below Html code in multipleUploadForm.php file.

<!doctype html>
<html>
  <head>
   <title>Codeigniter - Upload Multiple Files and Images Example - techbuzz.bvmsolution.com/</title>
  </head>
<body>
  
 <form method='post' action="<?php echo base_url() ?>MultipleUpload/Imageupload"  enctype='multipart/form-data'>
   
      <input type='file' name='files[]' multiple=""> <br/><br/>
      <input type='submit' value='Upload' name='upload' />
   
    </form>
   
</body>
</html> 

Step 6: Create view file to show success message name as ‘upload_success.php’ .Paste below code there:

<html>
<head>
<title>Success File</title>
</head>
<body>

<h3>Your files was successfully uploaded!</h3>
 <strong><?php if(isset($totalFiles)) echo "Successfully uploaded ".count($totalFiles)." files"; ?></strong>
</body>
</html>

Step 7: Now run your root directory to show your result.

Leave a Reply

Your email address will not be published. Required fields are marked *