Bulk Data Import feature is very useful for the data management section in the web application. Import feature helps to insert bulk data at once instead of one by one and reduce the time for inserting data in the database. Mostly, the CSV (comma-separated values) file format is used to import data. The CSV file holds the data in plain text format and can be easily imported in the server.
Import functionality makes it easy to insert a bunch of data in the database on a single click from the website. Using the fgetcsv() function, you can parse and import CSV file into the MySQL database in PHP. If your application built with CodeIgniter framework, a custom library needs to import CSV data in CodeIgniter. Because there is no system library available in CodeIgniter to import CSV data. In this tutorial, we will show you how to import CSV file data into MySQL database in CodeIgniter.
In the example code, we will import members data from CSV file in the database using CodeIgniter CSVReader library. The following process will be implemented to demonstrate the CodeIgniter CSV Import functionality.
Before getting started to implement the import CSV file to the database in CodeIgniter 3.x application, take a look at the files structure.
codeigniter_csv_import/ ├── application/ │ ├── controllers/ │ │ └── Members.php │ ├── libraries/ │ │ └── CSVReader.php │ ├── models/ │ │ └── Member.php │ └── views/ │ └── members/ │ └── index.php └── assets/ ├── css/ ├── images/ └── bootstrap/
To store the member’s data, a table is needed in the database. The following SQL creates a members
table with some basic fields in the MySQL database.
CREATE TABLE `members` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`phone` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`status` enum('Active','Inactive') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Active',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Based on the database table structure, the CSV file will have the 4 fields – Name, Email, Phone, Status. The format of the CSV file will similar to the following screen.
autoload.php
In the config/autoload.php
file, define the commonly used library and helper to load automatically on every request.
$autoload['libraries'] = array('database', 'session'); $autoload['helper'] = array('url');
CSVReader.php
The CSVReader library helps to read a CSV file and convert CSV data in an array in CodeIgniter 3.x application. Using this CSVReader class, you can import data from the CSV file in CodeIgniter.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); /** * CSV Reader for CodeIgniter 3.x * * Library to read the CSV file. It helps to import a CSV file * and convert CSV data into an associative array. * * This library treats the first row of a CSV file * as a column header row. * * * @package CodeIgniter * @category Libraries * @author CodexWorld * @license http://www.codexworld.com/license/ * @link http://www.codexworld.com * @version 3.0 */ class CSVReader { // Columns names after parsing private $fields; // Separator used to explode each line private $separator = ';'; // Enclosure used to decorate each field private $enclosure = '"'; // Maximum row size to be used for decoding private $max_row_size = 4096; /** * Parse a CSV file and returns as an array. * * @access public * @param filepath string Location of the CSV file * * @return mixed|boolean */ function parse_csv($filepath){ // If file doesn't exist, return false if(!file_exists($filepath)){ return FALSE; } // Open uploaded CSV file with read-only mode $csvFile = fopen($filepath, 'r'); // Get Fields and values $this->fields = fgetcsv($csvFile, $this->max_row_size, $this->separator, $this->enclosure); $keys_values = explode(',', $this->fields[0]); $keys = $this->escape_string($keys_values); // Store CSV data in an array $csvData = array(); $i = 1; while(($row = fgetcsv($csvFile, $this->max_row_size, $this->separator, $this->enclosure)) !== FALSE){ // Skip empty lines if($row != NULL){ $values = explode(',', $row[0]); if(count($keys) == count($values)){ $arr = array(); $new_values = array(); $new_values = $this->escape_string($values); for($j = 0; $j < count($keys); $j++){ if($keys[$j] != ""){ $arr[$keys[$j]] = $new_values[$j]; } } $csvData[$i] = $arr; $i++; } } } // Close opened CSV file fclose($csvFile); return $csvData; } function escape_string($data){ $result = array(); foreach($data as $row){ $result[] = str_replace('"', '', $row); } return $result; } }
The Members controller handles the CSV data import process.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Members extends CI_Controller { function __construct() { parent::__construct(); // Load member model $this->load->model('member'); // Load form validation library $this->load->library('form_validation'); // Load file helper $this->load->helper('file'); } public function index(){ $data = array(); // Get messages from the session if($this->session->userdata('success_msg')){ $data['success_msg'] = $this->session->userdata('success_msg'); $this->session->unset_userdata('success_msg'); } if($this->session->userdata('error_msg')){ $data['error_msg'] = $this->session->userdata('error_msg'); $this->session->unset_userdata('error_msg'); } // Get rows $data['members'] = $this->member->getRows(); // Load the list page view $this->load->view('members/index', $data); } public function import(){ $data = array(); $memData = array(); // If import request is submitted if($this->input->post('importSubmit')){ // Form field validation rules $this->form_validation->set_rules('file', 'CSV file', 'callback_file_check'); // Validate submitted form data if($this->form_validation->run() == true){ $insertCount = $updateCount = $rowCount = $notAddCount = 0; // If file uploaded if(is_uploaded_file($_FILES['file']['tmp_name'])){ // Load CSV reader library $this->load->library('CSVReader'); // Parse data from CSV file $csvData = $this->csvreader->parse_csv($_FILES['file']['tmp_name']); // Insert/update CSV data into database if(!empty($csvData)){ foreach($csvData as $row){ $rowCount++; // Prepare data for DB insertion $memData = array( 'name' => $row['Name'], 'email' => $row['Email'], 'phone' => $row['Phone'], 'status' => $row['Status'], ); // Check whether email already exists in the database $con = array( 'where' => array( 'email' => $row['Email'] ), 'returnType' => 'count' ); $prevCount = $this->member->getRows($con); if($prevCount > 0){ // Update member data $condition = array('email' => $row['Email']); $update = $this->member->update($memData, $condition); if($update){ $updateCount++; } }else{ // Insert member data $insert = $this->member->insert($memData); if($insert){ $insertCount++; } } } // Status message with imported data count $notAddCount = ($rowCount - ($insertCount + $updateCount)); $successMsg = 'Members imported successfully. Total Rows ('.$rowCount.') | Inserted ('.$insertCount.') | Updated ('.$updateCount.') | Not Inserted ('.$notAddCount.')'; $this->session->set_userdata('success_msg', $successMsg); } }else{ $this->session->set_userdata('error_msg', 'Error on file upload, please try again.'); } }else{ $this->session->set_userdata('error_msg', 'Invalid file, please select only CSV file.'); } } redirect('members'); } /* * Callback function to check file value and type during validation */ public function file_check($str){ $allowed_mime_types = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain'); if(isset($_FILES['file']['name']) && $_FILES['file']['name'] != ""){ $mime = get_mime_by_extension($_FILES['file']['name']); $fileAr = explode('.', $_FILES['file']['name']); $ext = end($fileAr); if(($ext == 'csv') && in_array($mime, $allowed_mime_types)){ return true; }else{ $this->form_validation->set_message('file_check', 'Please select only CSV file to upload.'); return false; } }else{ $this->form_validation->set_message('file_check', 'Please select a CSV file to upload.'); return false; } } }
The Member model handles the database related works (Fetch, Insert, and Update).
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Member extends CI_Model{ function __construct() { // Set table name $this->table = 'members'; } /* * Fetch members data from the database * @param array filter data based on the passed parameters */ function getRows($params = array()){ $this->db->select('*'); $this->db->from($this->table); if(array_key_exists("where", $params)){ foreach($params['where'] as $key => $val){ $this->db->where($key, $val); } } if(array_key_exists("returnType",$params) && $params['returnType'] == 'count'){ $result = $this->db->count_all_results(); }else{ if(array_key_exists("id", $params)){ $this->db->where('id', $params['id']); $query = $this->db->get(); $result = $query->row_array(); }else{ $this->db->order_by('id', 'desc'); if(array_key_exists("start",$params) && array_key_exists("limit",$params)){ $this->db->limit($params['limit'],$params['start']); }elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){ $this->db->limit($params['limit']); } $query = $this->db->get(); $result = ($query->num_rows() > 0)?$query->result_array():FALSE; } } // Return fetched data return $result; } /* * Insert members data into the database * @param $data data to be insert based on the passed parameters */ public function insert($data = array()) { if(!empty($data)){ // Add created and modified date if not included if(!array_key_exists("created", $data)){ $data['created'] = date("Y-m-d H:i:s"); } if(!array_key_exists("modified", $data)){ $data['modified'] = date("Y-m-d H:i:s"); } // Insert member data $insert = $this->db->insert($this->table, $data); // Return the status return $insert?$this->db->insert_id():false; } return false; } /* * Update member data into the database * @param $data array to be update based on the passed parameters * @param $condition array filter data */ public function update($data, $condition = array()) { if(!empty($data)){ // Add modified date if not included if(!array_key_exists("modified", $data)){ $data['modified'] = date("Y-m-d H:i:s"); } // Update member data $update = $this->db->update($this->table, $data, $condition); // Return the status return $update?true:false; } return false; } }
members/index.php
Initially, all the existing member’s data is fetched from the database and listed in the webpage.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CodeIgniter CSV Import</title>
<!-- Bootstrap library -->
<link rel="stylesheet" href="<?php echo base_url('assets/bootstrap/bootstrap.min.css'); ?>">
<!-- Stylesheet file -->
<link rel="stylesheet" href="<?php echo base_url('assets/css/style.css'); ?>">
</head>
<body>
<div class="container">
<h2>Members List</h2>
<!-- Display status message -->
<?php if(!empty($success_msg)){ ?>
<div class="col-xs-12">
<div class="alert alert-success"><?php echo $success_msg; ?></div>
</div>
<?php if(!empty($error_msg)){ ?>
<div class="col-xs-12">
<div class="alert alert-danger"><?php echo $error_msg; ?></div>
</div>
<?php } ?>
<div class="row">
<!-- Import link -->
<div class="col-md-12 head">
<div class="float-right">
<a href="javascript:void(0);" class="btn btn-success" onclick="formToggle('importFrm');"><i class="plus"></i> Import</a>
</div>
</div>
<!-- File upload form -->
<div class="col-md-12" id="importFrm" style="display: none;">
<form action="<?php echo base_url('members/import'); ?>" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" class="btn btn-primary" name="importSubmit" value="IMPORT">
</form>
</div>
<!-- Data list table -->
<table class="table table-striped table-bordered">
<thead class="thead-dark">
<tr>
<th>#ID</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php if(!empty($members)){ foreach($members as $row){ ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['status']; ?></td>
</tr>
<?php } }else{ ?>
<tr><td colspan="5">No member(s) found...</td></tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<script>
function formToggle(ID){
var element = document.getElementById(ID);
if(element.style.display === "none"){
element.style.display = "block";
}else{
element.style.display = "none";
}
}
</script>
</body>
</html>
CodeIgniter CRUD Operations with Search and Pagination
In the example script, we provided a simple way to import data from CSV file in CodeIgniter. Using our custom CSVReader library, you can import CSV or Excel file data to the MySQL database with CodeIgniter. Also, the functionality of this script can be easily enhanced to add more fields in the CSV file as per your needs.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
How to define used library and helper in autoload.php codeigniter 4? Because the App/Config/Autoload.php file in CI 3 and CI 4 is different
getting solution of my above question after using $keys = str_getcsv($this->fields[0]);
I am getting null once when i use column value with comma separate
excelent………….
Excelent. Very clear and functional. It works excelent. Thanks for your work
Hello my name is Saeed and I’m reading this topic from Iran … Thank you very much for the usefulness
Thanks, good