Press "Enter" to skip to content

How to upload excel and CSV file in CodeIgniter framework to MySQL


In this tutorial, we are going to upload an excel file and parse its data and store it in a database by using SimpleXLSX.

SimpleXlsx Class Parse and retrieve data from Excel XLSx files, and put them in an array. We are going to show you how to How to upload excel and CSV files in the CodeIgniter framework to MySQL PHP.

Before starting we need to get the SimpleXlsx Class from the GitHub and put that file in third_party directory of Codeigniter

In CodeIgniter, we need to create three file’s:-

1. For Model:- In the model, we need to create a method for the module, where we insert the data in a table.

class ExcelModel extend CI_Model{
   function excelstore($data){
       $this->db->insert('user', $data);

2. For Controller:- In the controller, we will add the class SimpleXlsx, and using this we will upload and parse them to the query.

class UploadExcel extends CI_Controller{
  function upload(){
    $filesop = [];
    require_once APPPATH."/third_party/SimpleXlsx.php";
    $file = $_FILES['csv']['tmp_name'];
    if($x = SimpleXLSX::parse($file)){
        $fields = $x->rows();
    $c = 0;//
    foreach($fields as $filesop){
        $data = array(
            'name' => $filesop[1],
            'mobile' => $filesop[2],
            'email' => $filesop[3],
            'city' => $filesop[0]
         if($c != 0){
             $i = $i + 1;
             $c = $c + 1;

3. For View:- In view, we will create a form by which we upload excel file.

<!DOCTYPE html>
<form enctype="multipart/form-data" method="post" role="form" action="<?= site_url('UploadExcel/upload') ?>">
    <div class="form-group">
      <label for="exampleInputFile">File Upload</label>
      <input type="file" name="file" id="file" size="150">
      <p class="help-block">Only Excel/CSV File Import.</p>
     <button type="submit" class="btn btn-default" name="submit" value="submit">Upload</button>

It’s done, now you can check the code if you have any doubt in running this code you can comment or contact me directly.

Check out our some PHP Projects:-

Check some more useful tutorial of php:-

How to use JWT in CodeIgniter for creating API Authorization token
Basic Difference between Props and State - React.js