Pro Tip : PHP has built in functions for reading and writing csv files

Just discovered this and I am mighty impressed. It seems that in PHP 5.1 and higher there are the functions fgetcsv() and fputcsv(). They make it pretty simple to read and write a csv file.

Here is a simple example which reads a csv file, adds a couple of fields and writes it to a new file.


2 thoughts on “Pro Tip : PHP has built in functions for reading and writing csv files

  1. Pete

    Also if you dont mind taking a bit of a performance hit leave out the 1000 from the line:

    while (($data = fgetcsv($inputfp, 1000, ",")) !== FALSE) {

    if you have long fields this will shorten them, in PHP 5 you can omit that

    Reply

Leave a Reply

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