Read CSV file using php

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&#91;$c&#93; . "<br />\n";
          }
      }
  fclose($handle);
?>

Hope this can help you!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s