How to read CSV file contents in php

Posted: June 5, 2012 in Programming
Tags: , , , ,

PHP makes it easy to read CSV files by using fgetcsv function.

Step 1 : Lets create a sample CSV file
Sample File
Step 2 : The file looks like this when opened with notepad
Notepad View
Step 3: Now let us write the coding in php to read the contents word by word


$file = fopen(“data.csv”,”r”); // open file in read mode
while(!feof($file)) // loop through end of file
$word = fgetcsv($file,1024); // get the content as array
print $word[0].$word[1].”
“; // print the array
fclose($file); // close the file

Step 4: The output looks like


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s