How To Read CSV File in CakePHP 4 Tutorial

Reading Time: 4 minutes
2,860 Views

Inside this article we will see the concept i.e How to read CSV file in cakephp 4. Article contains classified information. It will give the complete idea of CSV file reading in cakephp 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 cakephp 4 about CSV file reading.

Learn More –

Let’s get started.

CakePHP 4 Installation

To create a CakePHP project, run this command into your shell or terminal. Make sure composer should be installed in your system.

$ composer create-project --prefer-dist cakephp/app:~4.0 mycakephp

Above command will creates a project with the name called mycakephp.

CSV Data Preparation

Let’s consider a .csv file in application. We have a students.csv inside /webroot 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 .json file either in any folder within /webroot folder. But here we will read only by specifying at root of webroot.

Create Controller

Open project into terminal and run this command to create controller.

$ bin/cake bake controller Site --no-actions

It will create a file SiteController.php inside /src/Controller folder.

Assuming .csv file is stored into /webroot folder.

Open SiteController.php and write this complete code into it.

<?php

declare(strict_types=1);

namespace App\Controller;

class SiteController extends AppController
{
    public function initialize(): void
    {
        parent::initialize();
        $this->autoRender = false;
    }

    public function readData()
    {
        $students = [];

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

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

            fclose($open);
        }

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

Concept

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

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

    fclose($open);
}

Here, we are parsing students.csv file. WWW_ROOT is a CakePHP 4 constant which returns the path upto /webroot folder.

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

Add Route

Open routes.php from /config folder. Add this route into it.

//...

$routes->connect(
    '/csv-data',
    ['controller' => 'Site', 'action' => 'readData']
);

//...

Application Testing

Back to terminal and run this command to start development server.

$ bin/cake server

URL: http://localhost:8765/csv-data

We hope this article helped you to learn How To Read CSV File in CakePHP 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.