Codeigniter File Uploading: Single and Multiple file Uploading with Codeigniter

single-and-multiple-file-uploading-with-codeigniter

In this tutorial we will see how file uploading process will be done by using codeigniter file upload class.It is very easy to handle all file uploading processes with codeigniter upload class which comes with various inbuilt configuration.Let see step by step how to achieve this.

Key Features of Codeigniter Uploading Class:

  • Various Inbuilt Settings.
  • File Validations.
  • Easy to configure.

Single file Uploading:

Single file uploading, where you can upload one file at a time.Lets see how to implement single file uploading.

1. Create a view file named upload.php inside  application/views directory.


<!DOCTYPE html>
<html>
<head>
<title>TechAllType</title>
</head>
<body>
<center>
<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('<span style="color: #333333;">UploadController</span>/upload_file'); ?>">
<fieldset>
<legend>Personal Info</legend>
<table>
<tr>
<td>Name :</td><td><input type="text" name="name"></td>
</tr>
<tr>
<td>Email :</td><td><input type="text" name="email"></td>
</tr>
<tr>
<td>File :</td><td><input type="file" name="myfile"></td>
</tr>
<tr>
<td><input type="submit" name="upload"></td><td></td>
</tr>
</table>
</fieldset>
</form>
</center>
</body>
</html>

2. After done with the view file, next we need to create a controller. Create a controller file called UploadController.php inside application/controllers directory. Here we will load upload library.You can also load upload library globally inside application/config/autoload.php.

Please find below the controller’s code.

UploadController.php


<?php

class <span style="color: #333333;">UploadController</span> extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('UploadModel');
}
public function index()
{
$this->load->view('upload');
}
public function upload_file(){
$file_data='';

/* configuration and validations */
$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'];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('myfile')){
$file_data = $this->upload->data();
}
else{
// echo $this->upload->display_errors();
$this->session->set_flashdata('error',$this->upload->display_errors());
redirect('UploadController');
}
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'photo' => $file_data['file_name']
);
$this->UploadModel->insert_data($data);
$this->session->set_flashdata('success','Detail has been successfully inserted !');
redirect('UploadController');
}
}
?>

3. Next, You need to create a model. Purpose of create a model is to insert all file detail into database, which is being uploaded.Here is how code looks.

UploadModel.php


<?php
class UploadModel extends CI_Model
{
function __construct()
{
parent:: __construct();
}
public function insert_data($data)
{
$this->db->insert('upload',$data);
}
}
?>

Multiple file Uploading:

Multiple file uploading, where you can upload multiple file at a time.Lets see how to implement multiple file uploading.

1. View: MultipleUpload.php


<!DOCTYPE html>
<html>
<head>
<title>TechAllType</title>
</head>
<body>
<center>
<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('<span style="color: #333333;">MultipleFileUploadController</span>/upload_file'); ?>">
<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>

There is no big difference between single and multiple file input only need to include a multiple attribute with input file type.

2. Controller: MultipleFileUploadController.php


<?php

class MultipleFileUploadController extends CI_Controller
{

function __construct()
{
parent::__construct();
$this->load->model('MultipleFileUploadModel');
}
public function index()
{
$this->load->view('MultipleUpload');
}
public function upload_file(){

$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('MultipleFileUploadController<span style="font-size: 1rem;">'</span><span style="font-size: 1rem;">);</span>

}
}
$this->MultipleFileUploadModel->insert_data($data);
$this->session->set_flashdata('success','details has been successfully inserted !');
redirect('MultipleFileUploadController');
}
}
?>

3. Model: MultipleFileUploadModel


<?php
class MultipleFileUploadModel extends CI_Model
{
function __construct()
{
parent:: __construct();
}
public function insert_data($data)
{
$this->db->insert_batch('multiple_file_upload',$data);
}
}
?>

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion. if you face any issue with this code 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: , , , , , ,