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.

Multi File Upload example in codeigniter

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.

upload.php

<!DOCTYPE html>
<html>
<head>
<title>Multiple File Upload in Codeigniter</title>
</head>
<body>
<center>
<h3><u>Multiple File Upload in Codeigniter</u></h3>
<h4><?php
if(!empty($this->session->flashdata('success'))){
echo $this->session->flashdata('success');
}
if(!empty($this->session->flashdata('error'))){
echo $this->session->flashdata('error');
}

?></h4>
<form method="post" enctype="multipart/form-data" action="<?php echo base_url('MultipleFileUploadController/upload'); ?>">
<fieldset>
<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>
</fieldset>
</form>
</center>
</body>
</html>

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.

</pre>
class MultipleFileUploadController extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('MultipleFileUploadModel');
}
public function index()
{
$this->load->view('upload');
}
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);
$this->upload->initialize($config);
$this->upload->do_upload('file');
if($this->upload->do_upload('file')){
$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');
}else{
$this->session->set_flashdata('error',$this->upload->display_errors());
redirect('Upload/index');
}
}
$this->MultipleFileUploadModel->save($data);
$this->session->set_flashdata('success','data has successfully inserted !');
redirect('MultipleFileUploadController');
}
}
?>
<pre>

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.


<?php
class MultipleFileUploadModel extends CI_Model
{
function __construct()
{
parent:: __construct();
}
public function save($data)
{
$this->db->insert_batch('multiple_file_upload',$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!

I am Hitesh from Jamshedpur (India). I have been working as a Web Application Developer from last 4+ years. I love diverse and attention catching web presence for a variety of users. Also I love to learn new things in Web Development.

Tags: , , , , ,