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.

codeigniter_csv_import/
├── application/
│   ├── controllers/
│   │   └── Members.php
│   ├── libraries/
│   │   └── CSVReader.php
│   ├── models/
│   │   └── Member.php
│   └── views/
│       └── members/
│           └── index.php
└── assets/
    ├── css/
    ├── images/
    └── bootstrap/

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` (
 `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;

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.

codeigniter-import-sample-csv-file-format-codexworld

Config

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

Libraries

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.

  • 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.
<?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;
    }   
}

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.
<?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;
        }
    }
}

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.
<?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;
    }
}

Views

members/index.php
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">
<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

Conclusion

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

7 Comments

  1. Kyra Said...
  2. Laxman Mishra Said...
  3. Laxman Mishra Said...
  4. Rian Hardainto Said...
  5. ANIBAL MAURICIO PACHECO GONZALEZ Said...
  6. Saeed Farhadi Said...
  7. Photoreading Said...

Leave a reply

keyboard_double_arrow_up