CodeIgniter Upload CSV File into MYSQL Database

In this example, I show you how to import all CSV file data insert into the database. Here you need to first upload a file then read the file, and set CSV column as per the table column so your data import completely. Make uploads folder for save all the uploaded excel file in application directory

#1 Make Controller file in your controller folder 

The Filename is Add_csv.php  copy and paste below code in this file

<?php

class Add_csv extends CI_Controller
{

public function index()
{
$this->load->helper(‘form’);
$this->load->view(‘user/import’);
}

public function importFile()
{
$this->load->model(‘Csv_data’);

//file upload Start

$config[‘upload_path’] = ‘./uploads’;
$config[‘allowed_types’] = ‘csv’;

$this->load->library(‘upload’,$config);

$this->upload->do_upload();

$image=$this->upload->data(‘file_name’);
$image_path = base_url(“uploads/”.@$data[‘raw_name’].@$data[‘file_ext’]);

//End file upload

// file insert data Start
if(!empty($_FILES[“userfile”][“tmp_name”]))
{
$filename=$_FILES[“userfile”][“tmp_name”];

if($_FILES[“userfile”][“size”] > 0)
{

$file = fopen($filename, “r”);

$no=1;
while (($importdata = fgetcsv($file, 10000, “,”)) !== FALSE)
{
if($no==1){}else{

$data = array(
‘A’ => $importdata[0],
‘B’ => $importdata[1],
‘C’ => $importdata[2],
‘D’ => $importdata[3],
‘E’ => $importdata[4],
‘F’ => $importdata[5],
‘G’ => $importdata[6],
‘H’ => $importdata[7],
‘I’ => $importdata[8]
);
$insert = $this->Csv_data->insertexcel($data);

//echo “<pre>”;
//print_r($data);
//exit();
}
$no++;
}
fclose($file);
//exit();

$this->session->set_flashdata(‘feedback’,’File imported successfully..’);
$this->session->set_flashdata(‘feedback_class’,’alert-success’);
redirect(‘Add_csv/index’);
}
else
{
$this->session->set_flashdata(‘feedback’,’Something went wrong..’);
$this->session->set_flashdata(‘feedback_class’,’alert-danger’);
redirect(‘Add_csv/index’);
}
}
else
{
$this->session->set_flashdata(‘feedback’,’File Is Empty’);
$this->session->set_flashdata(‘feedback_class’,’alert-danger’);
redirect(‘Add_csv/index’);
}
}

}

?>

#2 Make View file in your view folder

 The file name is import.php, Copy and paste below code in your view file

<section id=”main-content”>
<section class=”wrapper”>
<h3><i class=”fa fa-angle-right”></i> CSV Import</h3>
<!– page start–>
<div>
<?php echo form_open_multipart(‘Add_csv/importFile’); ?>
<aside class=”col-lg-6 mt” style=” margin-left: 283px; border-style: inherit ; padding-left: 10px;border: 1px solid #ccc!important;background-color: white;border-top-left-radius: 4px;border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;”>
<h4><i class=”pe-7s-stopwatch”></i> &#160; CSV Import</h4>
<br/>

<?php if($feedback = $this->session->flashdata(‘feedback’)):
$feedback_class=$this->session->flashdata(‘feedback_class’);
?>

<div class=”alert alert-dismissible <?= $feedback_class ?>”>
<b><?= $feedback ?></b>
</div></br>

<?php endif; ?>

<div class=”form-group”>
File :
<?php echo form_input([‘name’=>’userfile’,’type’=>’file’,’class’=>’form-control’]) ?>
</div>
</br>
</br>
<div class=”form-group”>
<button type=”submit” class=”btn btn-facebook” style=”width: 100px”>Import</button>&#160;
</div>
</aside></br>
<?php echo form_close(); ?>
</div>
</br>

<!– page end–>
</section><! –/wrapper –>
</section><!– /MAIN CONTENT –>

 

#3 Make Model file in your model folder

The file name is Csv_data .php, Copy and paste below code in your model file 

<?php
class Csv_data extends CI_Model
{

public function insertexcel($data)
{

$this->db->insert(‘table name’, $data); // make a table with 8 column in your database 
return TRUE;
}

}

?>

 

Leave a Reply

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