How To Read CSV File in CakePHP 4 Tutorial

Reading Time: 4 minutes

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 –

Sanjay Kumar,,Male
Ashish Kumar,,Male
Vijay Rohila,,Male
Dhananjay Negi,,Male
Monu Thakur,,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.



namespace App\Controller;

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

    public function readData()
        $students = [];

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

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


        echo "<pre>";


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

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


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.


    ['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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more