How To Read CSV File in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 4 minutes
278 Views

Inside this article we will see the concept i.e How to read CSV file in codeigniter 4. Article contains classified information. It will give the complete idea of CSV file reading in codeigniter 4.

This tutorial will be super easy to understand and it’s steps are easier to implement in your code as well. If you learn reading CSV file here, you can use the same concept in data seeding to database via CSV file. This is step by step tutorial in codeigniter 4 about CSV file reading.

Learn More –

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system.

To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


.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. We will read only by specifying the path.


Create Controller

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.

Assuming /writable folder.

Open SiteController.php 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.

Now, we can insert the .csv file data into database etc.


Create Route

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

//...

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

//...

Application Testing

Start development server:

$ php spark serve

URL: http://localhost:8080/data

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

Buy Me a Coffee

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.

Learn Web Development Courses @ $4 only.
Web Development Courses @ $4