Bunch Files Upload: Codeigniter Multi Files Upload Example

Bunch Files Upload: Codeigniter Multi Files Upload Example

How upload bunch of files into server? In this tutorial we will see how to achieve this. Codeigniter upload library allows to upload files easily. Codeigniter’s Upload library comes with many configurations.Here is the full example of multiple file upload.

Creating Views:

Create a view file called upload.php views inside application/views directory.Here you need to define input file type and inside file type input, put multiple attribute which is new in HTML5. Multiple attribute of input file allows multiple file selection through file explorer window.Please find below the code of view.


<!DOCTYPE html>
<title>Multiple File Upload in Codeigniter</title>
<h3><u>Multiple File Upload in Codeigniter</u></h3>
echo $this->session->flashdata('success');
echo $this->session->flashdata('error');

<form method="post" enctype="multipart/form-data" action="<?php echo base_url('MultipleFileUploadController/upload'); ?>">
<legend>Personal Info</legend>
Gallery Id: <input type="text" name="gallery_id"><br><br>
File : <input type="file" name="myfile[]" multiple="multiple"><br><br>
<input type="submit" name="upload"><br>

Creating Controller and Multi Files Upload setup:

Next, Create a controller file named MultipleFileUploadController.php views inside application/controllers directory. Here we will load upload library and put all functions of multiple uploading.Here is how code looks.

class MultipleFileUploadController extends CI_Controller
function __construct()
public function index()
public function upload(){

$count = count($_FILES['myfile']['name']);
for ($i=0; $i < $count; $i++) {
$_FILES['file']['name'] = $_FILES['myfile']['name'][$i];
$_FILES['file']['type'] = $_FILES['myfile']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['myfile']['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES['myfile']['error'][$i];
$_FILES['file']['size'] = $_FILES['myfile']['size'][$i];
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$config['file_name'] = $_FILES['myfile']['name'][$i];
$this->load->library('upload', $config);
$file_data = $this->upload->data();
$data[$i]['gallery_id'] = $this->input->post('gallery_id');
$data[$i]['file_name'] = $file_data['file_name'];
$data[$i]['created_date'] = date('Y-m-d h:i:s');
$this->session->set_flashdata('success','data has successfully inserted !');

Creating Model:

Now, Create a model file named MultipleFileUploadModel.php views inside application/models directory. Here in model we will insert all file details into database table which is coming through an array. To insert array data here we will use insert_array_batch query builder class of codeigniter.

class MultipleFileUploadModel extends CI_Model{
function __construct(){
parent:: __construct();
public function save($data){

That’s all.Now you can test it finally.

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – let us know. We’d love to help!