It's All About Me

make them benefit

Read CSV file using php

leave a comment »

I believe, it’s pretty simple and it’s widely used. Yes,  CSV file is widely used for data importing or exporting in web application specially application built using php. So sometimes it’s needed to create a CSV file from data and vice-versa.  All version of PHP has a nice built in method by which you can read a csv file(comma, tab, semicolon delimited) easily. Try with the following code which i taken from php manual directly without any modification -

<?php
  $row = 1;
  $handle = fopen("test.csv", "r");
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
      $num = count($data);
      echo "<p> $num fields in line $row: <br /></p>\n";
      $row++;
          for ($c=0; $c < $num; $c++) {
          echo $data[$c] . "<br />\n";
          }
      }
  fclose($handle);
?>

Hope this can help you!

Written by kodegeek

July 6, 2009 at 5:36 pm

Posted in Php

Leave a Reply