How To Read CSV File in PHP Tutorial

Reading Time: 3 minutes
3,266 Views

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

This tutorial will be super easy to understand and it’s steps are easier to implement in your code as well. If you will 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 PHP about CSV file reading.

How to convert a CSV into a PHP array, we will see the whole concept here in a very easy way.

Learn More –

Let’s get started.


Create an Application

Create a folder php-csv in your localhost directory.

Inside this folder, create a file called index.php.

Suppose we have a CSV file as students.csv

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

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

<?php

$students = [];

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

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

    fclose($open);
}

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

Application Testing

Open browser and hit this URL: http://localhost/php-csv

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