CodeIgniter 4 How To Read Parse CSV File Tutorial

Reading Time: 4 minutes
3,688 Views

CSV (Comma-Separated Values) files serve as a widely used format for storing tabular data, and handling them within web applications is a common requirement. In CodeIgniter 4, parsing CSV files allows developers to extract, process, and manipulate tabular data seamlessly.

In this tutorial, we’ll see the comprehensive process of reading and parsing CSV files using CodeIgniter 4. This functionality empowers developers to efficiently handle tabular data stored in CSV format within their applications.

Read More: CodeIgniter 4 How To Redirect with Flashdata Example Tutorial

Let’s get started.

CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.

Environment (.env) Setup

When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Either we can do via renaming file as simple as that. Also we can do by terminal command.

Open project in terminal

cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
 
CI_ENVIRONMENT = development

Now application is in development mode.

CSV Data Preparation

Let’s consider a .csv file in application. We have a students.csv inside /writable folder.

If we open that file, it is looking like this –

Name,Email,Gender
Sanjay Kumar,sanjay@gmail.com,Male
Ashish Kumar,ashish@gmail.com,Male
Vijay Rohila,vijay@gmail.com,Male
Dhananjay Negi,dj@gmail.com,Male
Monu Thakur,monu@gmail.com,Female

You can place this .csv file either in /writable folder or /public folder.

Read More: CodeIgniter 4 How To Read XML Data to JSON Tutorial

Create Controller

Assuming .csv file is stored inside /writable folder.

Open project into terminal and run this spark command.

php spark make:controller Site --suffix

It will create a file SiteController.php inside /app/Controllers folder.

Open file and write this complete code into it.

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class SiteController extends BaseController
{
    public function index()
    {
        $students = [];

        if (($open = fopen(WRITEPATH . "students.csv", "r")) !== FALSE) {

            while (($data = fgetcsv($open, 1000, ",")) !== FALSE) {
                $students[] = $data;
            }

            fclose($open);
        }

        echo "<pre>";
        print_r($students);
    }
}

Concept

if (($open = fopen(WRITEPATH . "students.csv", "r")) !== FALSE) {

    while (($data = fgetcsv($open, 1000, ",")) !== FALSE) {
        $students[] = $data;
    }

    fclose($open);
}

Here, we are parsing students.csv file. WRITEPATH is a CodeIgniter 4 constant which returns the path upto /writable folder.

If you have taken /public folder then you should use FCPATH in place of it.

Once you get data from .csv into array format, you can insert into your database as well.

Create Route

Open Routes.php from /app/Config folder. Add this route into it.

//...

$routes->get('/data', 'SiteController::index');

//...

Application Testing

Open project terminal and start development server via command:

php spark serve

Read More: CodeIgniter 4 How To Send Email with Attachments Tutorial

URL: http://localhost:8080/data

That’s it.

We hope this article helped you to learn about How To Read Parse CSV File in CodeIgniter 4 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.