PHP 

This tutorial shows how to read from csv file and write to a csv file using php. 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:

The below code snippets first check if the file exists and has required permission for reading.

Then we 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);
}

 

Code snippets for writing to a csv file:

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 data in read.csv file which needs to be read:

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

Source code for reading from read.csv file and write to write.csv file:

Here in the below code snippets we read from csv file and display those content from csv file into HTML table.

At the same time we write the cab content into another csv file.

Therefore the below source code shows how to read from csv file and write to a csv file using php.

<?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 csvparser.php
 */

Output file write.csv file:

The below content is the output of the written csv content into another 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

That’s all. Thanks for your reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

2 thoughts on “Read from and write to a csv file using php

  1. I have a scenario wherein admns from other departments can upload a (csv) file to server. Data structure is
    name,date,comments. Here in the details column, there may be many occurrences of comma.
    Example.
    Tom,3/12/2017, Logged the data, ordered 14 pieces

    As you can see there is comma in the 3rd field.

    As a super user, I need to read the csv file,check this column and replace comma with another char and then load in mysql db.

    Can you tell me how to do?

Leave a Reply

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