YouTube Icon

Code Playground.

How to Import CSV File Data into MySQL Database in CodeIgniter


How to Import CSV File Data into MySQL Database in CodeIgniter

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.

  • Fetch all the members data from the database and listed on the web page.
  • CSV file upload form.
  • Parse and import CSV file data in the database.

Before getting started to implement the import CSV file to the database in CodeIgniter 3.x application, take a look at the files structure.

Create Database Table

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` (
 `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',
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CSV File Format

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.


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');


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.

  • parse_csv() – Parses a CSV file and returns data as an array.
    • Open the CSV file in read-only mode using PHP fopen() function.
    • Parse data from the opened CSV file using PHP fgetcsv() function.
    • Create an array with fields and value of the CSV data.
    • Close the opened CSV file.
    • Return CSV data as an array format.
defined('BASEPATH') OR exit('No direct script access allowed');

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;
    function parse_csv($filepath){
        // If file doesn't exist, return false
            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;
        // Close opened CSV file
        return $csvData;

    function escape_string($data){
        $result = array();
        foreach($data as $row){
            $result[] = str_replace('"', '', $row);
        return $result;

Controllers (Members.php)

The Members controller handles the CSV data import process.

  • __construct() – Loads the required library (form_validation), helper (file), and model (member).
  • index() – List the members data.
    • Retrieve status messages from SESSION.
    • Fetch the records from the database using getRows() method of Member model.
    • Pass the members data to the list view.
  • import() – Import CSV or Excel file data to the database.
    • The submitted file is validated to check whether it is a valid CSV file (using the Form Validation library).
    • If the file is uploaded, data is parsed from the CSV file using the CSVReader library.
    • Based on the email address, the CSV data is inserted/updated in the database.
    • The status message is stored in the SESSION.
    • The page is redirected to the list view.

file_check() – This is a callback function for file upload validation, checks and validate the file input field’s value and type (.csv).

  • The get_mime_by_extension() function of File helper is used to identify the MIME type of the selected file.
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Members extends CI_Controller {
    function __construct() {
        // Load member model
        // Load form validation library
        // Load file helper
    public function index(){
        $data = array();
        // Get messages from the session
            $data['success_msg'] = $this->session->userdata('success_msg');
            $data['error_msg'] = $this->session->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
            // 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
                    // Load CSV reader library
                    // Parse data from CSV file
                    $csvData = $this->csvreader->parse_csv($_FILES['file']['tmp_name']);
                    // Insert/update CSV data into database
                        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);
                                // Insert member data
                                $insert = $this->member->insert($memData);
                        // 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);
                    $this->session->set_userdata('error_msg', 'Error on file upload, please try again.');
                $this->session->set_userdata('error_msg', 'Invalid file, please select only CSV file.');
     * 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/', '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;
                $this->form_validation->set_message('file_check', 'Please select only CSV file to upload.');
                return false;
            $this->form_validation->set_message('file_check', 'Please select a CSV file to upload.');
            return false;

Models (Member.php)

The Member model handles the database related works (Fetch, Insert, and Update).

  • __construct() – Define the table name.
  • getRows() – Fetch the members data from the database based on the conditions specified in the $params. Returns the records on success.
  • insert() – Insert member data in the database. Returns the row ID on success, and FALSE on error.
  • update() – Update member data in the database based on the specified condition. Returns TRUE on success, and FALSE on error.
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member extends CI_Model{
    function __construct() {
        // Set table name
        $this->table = 'members';
    function getRows($params = array()){
        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();
            if(array_key_exists("id", $params)){
                $this->db->where('id', $params['id']);
                $query = $this->db->get();
                $result = $query->row_array();
                $this->db->order_by('id', 'desc');
                if(array_key_exists("start",$params) && array_key_exists("limit",$params)){
                }elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){
                $query = $this->db->get();
                $result = ($query->num_rows() > 0)?$query->result_array():FALSE;
        // Return fetched data
        return $result;
    public function insert($data = array()) {
            // 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;
    public function update($data, $condition = array()) {
            // 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;


Initially, all the existing member’s data is fetched from the database and listed in the webpage.

  • An Import button is placed at the top of the data list.
  • Once the Import button is clicked, a form is appeared with an input field to select a CSV file.
  • The selected file is posted to the import() method of Members controller.
  • If the CSV data is imported to the database successfully, the inserted member’s data will be appended in the list.
  • (Optional) The Bootstrap 4 library is used for styling the HTML table and form. So, include the CSS file of the Bootstrap library and include the custom stylesheet file (if any).
  • formToggle() – This is a JavaScript function, helps to display or hide the file upload form on Import button click.
<!DOCTYPE html>
<html lang="en">
    <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'); ?>">
<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>
    <?php if(!empty($error_msg)){ ?>
    <div class="col-xs-12">
        <div class="alert alert-danger"><?php echo $error_msg; ?></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>
        <!-- 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">
        <!-- Data list table -->
        <table class="table table-striped table-bordered">
            <thead class="thead-dark">
                <?php if(!empty($members)){ foreach($members as $row){ ?>
                    <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>
                <?php } }else{ ?>
                <tr><td colspan="5">No member(s) found...</td></tr>
                <?php } ?>

function formToggle(ID){
    var element = document.getElementById(ID);
    if( === "none"){ = "block";
    }else{ = "none";


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.