How to upload Multiple Files and Images in CodeIgniter?

Today, We want to share with you multiple upload codeigniter.In this post we will show you Codeigniter – Upload Multiple File and Image Example, hear for How To Insert Multiple Image In Database Using Codeigniter? we will give you demo and example for implement.In this post, we will learn about How To Upload Multiple Images In Codeigniter? with an example.

Multiple files upload in Codeigniter

Step 1: Download Fresh Codeigniter 3

download fresh version of Codeigniter 3
download from here Free Download Codeigniter 3

Step 2: Add Route

application/config/routes.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');


$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['image-upload'] = 'ProfilePicUpload';
$route['image-upload/post']['post'] = "ProfilePicUpload/storeProfileDP";

Step 3: Create Controller

application/controllers/ProfilePicUpload.php

<?php
  
class ProfilePicUpload extends CI_Controller {
   
   /**
    * Manage __construct
    *
    * @return Response
   */
   public function __construct() { 
      parent::__construct(); 
      $this->load->helper('url'); 
   }
  
   /**
    * Manage index
    *
    * @return Response
   */
   public function index() { 
      $this->load->view('profileDpFormLoadData'); 
   } 
    
   /**
    * Manage storeProfileDP
    *
    * @return Response
   */
   public function storeProfileDP() { 
   
      $data = [];
   
      $count = count($_FILES['profiles']['name']);
    
      for($i=0;$i<$count;$i++){
    
        if(!empty($_FILES['profiles']['name'][$i])){
    
          $_FILES['file']['name'] = $_FILES['profiles']['name'][$i];
          $_FILES['file']['type'] = $_FILES['profiles']['type'][$i];
          $_FILES['file']['tmp_name'] = $_FILES['profiles']['tmp_name'][$i];
          $_FILES['file']['error'] = $_FILES['profiles']['error'][$i];
          $_FILES['file']['size'] = $_FILES['profiles']['size'][$i];
  
          $config['upload_path'] = 'uploads/'; 
          $config['allowed_types'] = 'jpg|jpeg|png|gif';
          $config['max_size'] = '5000';
          $config['file_name'] = $_FILES['profiles']['name'][$i];
   
          $this->load->library('upload',$config); 
    
          if($this->upload->do_upload('file')){
            $dpStoreImg = $this->upload->data();
            $filename = $dpStoreImg['file_name'];
   
            $data['listProfiles'][] = $filename;
          }
        }
   
      }
   
      $this->load->view('profileDpFormLoadData', $data); 
   }
  
} 
?>

Step 4: Create View

application/views/profileDpFormLoadData.php

<!doctype html>
<html>
  <head>
   <title>Codeigniter - Upload Multiple Files and Images Example - www.pakainfo.com</title>
  </head>
<body>
  
    <strong><?php if(isset($listProfiles)) echo "Good Luck Successfully uploaded ".count($listProfiles)." profiles"; ?></strong>
       
    <form method='post' action='/image-upload/post' enctype='multipart/form-data'>
   
      <input type='file' name='profiles[]' multiple=""> <br/><br/>
      <input type='submit' value='Upload' name='upload' />
   
    </form>
   
</body>
</html>

application/config/config.php

$config['base_url'] = 'http://localhost:8000/';

I hope you get an idea about Multiple Files and Images in CodeIgniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.