Read from and write to a csv file using php

This tutorial shows how to read from a csv file and write to an another csv file. In this example I have shown how to read a csv file and display those values. I have also written those csv entries to another csv file.

Code snippets for reading a csv file

$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);
}

 

Continue reading “Read from and write to a csv file using php”