Reading .CSV file in PHP

By: David Sklar  

You have data in comma-separated values (CSV) format, for example a file exported from Excel or a database, and you want to extract the records and fields into a format you can manipulate in PHP.

If the CSV data is in a file (or available via a URL), open the file with fopen( )and read in the data with fgetcsv( ). This prints out the data in an HTML table:

$fp = fopen('sample2.csv','r') or die("can't open file");
print "<table>\n";
while($csv_line = fgetcsv($fp,1024)) {
    print '<tr>';
    for ($i = 0, $j = count($csv_line); $i < $j; $i++) {
        print '<td>'.$csv_line[$i].'</td>';
    print "</tr>\n";
print '</table>\n';
fclose($fp) or die("can't close file");

The second argument to fgetcsv( ) must be longer than the maximum length of a line in your CSV file. (Don't forget to count the end-of-line whitespace.) If you read in CSV lines longer than 1K, change the 1024 used in this recipe to something that accommodates your line length.

You can pass fgetcsv( ) an optional third argument, a delimiter to use instead of a comma (,). Using a different delimiter however, somewhat defeats the purpose of CSV as an easy way to exchange tabular data.

Don't be tempted to bypass fgetcsv( ) and just read a line in and explode()on the commas. CSV is more complicated than that, in order to deal with embedded commas and double quotes. Using fgetcsv( ) protects you and your code from subtle errors.

Archived Comments

1. I must say the blog post is just useful for everyone else reading it because the information and kno
View Tutorial          By: Intraday tips at 2013-12-03 08:43:29

2. excellent, thanks a lot..............
View Tutorial          By: yellareddy at 2013-03-12 06:56:12

3. very usefull i have found simple and nice code here shows read and write operations on csv

View Tutorial          By: sourabh at 2013-01-31 06:47:58

4. Great!!!!!!!!!!!!!!!!!!!!
View Tutorial          By: Raghbendra Nayak at 2012-03-28 12:54:35

5. is there any way to get the coloumn names of csv files
View Tutorial          By: martin at 2011-10-16 09:16:22

6. Great tip... I am currently using it on my site - and it works like a charm
View Tutorial          By: Admin at 2011-08-30 12:13:03

7. i want excel file upload and stored into database and genreat pdf in php
View Tutorial          By: excel file upload and genreat pdf in php at 2011-07-25 08:24:50

8. Thank you very much for this tip.
View Tutorial          By: Lise at 2011-07-15 13:27:23

9. thank for this
View Tutorial          By: ankit at 2011-07-15 06:48:21

10. great thanks a lottttttttttttttttt
View Tutorial          By: reshma at 2011-06-13 04:35:44

11. Really just brilliant...thanks
View Tutorial          By: Ash Singh at 2011-02-21 04:47:20

12. Nice Script,
It really works for me and saved lots of time

View Tutorial          By: Avi at 2010-09-11 02:25:22

13. This works great.. :)

thanks a lot

View Tutorial          By: Ravi at 2010-07-03 23:34:46

14. i want to know how to read a file inside zipfile.
for eg. i want to read csv from a zipfile w

View Tutorial          By: iman at 2009-11-26 12:50:21

15. Many many thanks. It is very very helpful. Again thanks many many....
View Tutorial          By: Mehedi Hasan at 2009-09-15 21:41:51

16. thank for this script
because i will work in my mca project

View Tutorial          By: r at 2009-06-27 05:07:31

17. Thanks for this tip -
I'm currently working on a project and have found this tip very handy.

View Tutorial          By: Helen at 2009-06-16 05:28:24

18. It's very very useful for me. thank you.
View Tutorial          By: thirumalai at 2009-01-28 03:25:25

Most Viewed Articles (in PHP )

Convert XML to CSV in PHP

for Loops in PHP

PHP file upload (Large Files)

PHP file upload prompts authentication for anonymous users

isset() function in PHP

Retrieve multiple rows from mysql and automatically create a table in PHP

Running different websites on different versions of PHP in Windows 2003 & IIS6 platform


PHP 5.1.4 INSTALLATION on Solaris 9 (Sparc)

Function to force strict boolean values in PHP

Building PHP 5.x with Apache2 on SuSE Professional 9.1/9.2

Installing PHP 5.x with Apache 2.x on HP UX 11i and configuring PHP 5.x with Oracle 9i

Cannot load /usr/local/apache/libexec/ into server:

Setting up PHP in Windows 2003 Server IIS7, and WinXP 64

Malware: global $ob_starting;

Latest Articles (in PHP)

Comment on this tutorial