Read from and write to a CSV file using PHP

This tutorial shows how to read from a csv file and write to a csv file using PHP programming language. In this example I have shown how to read a csv file and display those values on the browser when you run the PHP file. I have also written those csv entries into another output csv file.

There are few advantages of CSV data:

  • CSV format is considered to be standard format
  • CSV is smaller in size and faster to handle
  • CSV is simple to implement and easy to parse
  • CSV is human readable and easy to edit manually
  • CSV is processed by almost all applications

Prerequisites

PHP 7.4.3

CSV File Reading Example

In the below code snippets, first I check if the csv file exists and has required permission for reading.

Then you need to iterate for each line and print the data using array index. The fgetcsv() function retrieves comma separated values into array from each line.

$file_name = "read.csv";
if (($handle = fopen($file_name, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
        echo $data[0] or $data[1] or $data[2] etc.
        //more code
    }
    fclose($handle);
}

CSV File Writing Example

The below code snippets tell that we first open a file with write mode. Make sure that the file is accessible and has the required permission for writing to it.

Then we write the comma separated line to csv file using fputcsv() function.

$fp = fopen('write.csv', 'w');

//$infos array containing comma separated values
foreach ($infos as $info) {
    fputcsv($fp, array($info));
}

fclose($fp);

Sample CSV Data

Here is the sample csv data which I am going to read, display and write to another csv file.

abc xyz,bdhur@mail.com,20/05/1874
abe xuz,sumaut@mail.com,05/04/1987
dfa jyz,akash@hr.com,04/07/1864
fff hgh,arya.p@tech.com,05/05/1905

Read and Write CSV File

Here in the below code snippets that will read from csv file and display those content from csv file into HTML table on the browser. At the same time the below code writes the csv content into another csv file.

<?php

$infos = array();
$file_name = "read.csv";
if (($handle = fopen($file_name, "r")) !== FALSE) {
    echo '<table border="1"><thead>';
    echo '<th>Name</th><th>Email</th><th>Date Of Birth</th>';
    echo '</thead>';
    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
        echo '<tr><td>' . $data[0] . '</td><td>' . $data[1] . '</td><td>' . $data[2] . '</td></tr>';
        $infos[] = $data[0] . ',' . $data[1] . ',' . $data[2];
    }
    echo '</tbody></table>';
    fclose($handle);
}

$fp = fopen('write.csv', 'w');

foreach ($infos as $info) {
    fputcsv($fp, array($info), ',', ' ');
}

fclose($fp);
/*
 * End of php-read-write-csv-file.php
 */

Output CSV File

The below content is the output of the csv content written into file.

 abc  xyz,bdhur@mail.com,20/05/1874
 abe  xuz,sumaut@mail.com,05/04/1987
 dfa  jyz,akash@hr.com,04/07/1864
 fff  hgh,arya.p@tech.com,05/05/1905

Output on Browser

Hitting the URL http://localhost/php-read-write-csv-file/php-read-write-csv-file.php on browser will give you the following output:

php read write csv file

Source Code

Download

2 thoughts on “Read from and write to a CSV file using PHP

  1. Read from and write to a CSV file using PHP

    Hallo Roy Soumitra
    I can not download read/write CSV/PHP from Git.
    xml-csv-xml I was able to download.
    My interests are: edit data in table with php.
    First, I want to test if you got this email.
    I then have more questions about php.

    Best Regards, Karl Hegetschweiler, 94116 Germany, Bavaria- Forest

Leave a Reply

Your email address will not be published. Required fields are marked *