Reading .CSV file in PHP
By: David Sklar Printer Friendly Format
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.
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
PHP code to write to a CSV file for Microsoft Applications
PHP code to write to a CSV file from MySQL query
PHP code to import from CSV file to MySQL
Password must include both numeric and alphabetic characters - Magento
Resume or Pause File Uploads in PHP
Multiple File Upload in PHP using IFRAME
Error: Length parameter must be greater than 0
PHP file upload with IIS on windows XP/2000 etc
PHP file upload prompts authentication for anonymous users
Exception in module wampmanager.exe at 000F15A0 in Windows 8
Count occurrences of a character in a String in PHP
Archived Comments
1. It's very very useful for me. thank you.
View Tutorial By: thirumalai at 2009-01-28 03:25:25
2. Thanks for this tip -
I'm currently workin
View Tutorial By: Helen at 2009-06-16 05:28:24
3. thank for this script
because i will work
View Tutorial By: r at 2009-06-27 05:07:31
4. Many many thanks. It is very very helpful. Again t
View Tutorial By: Mehedi Hasan at 2009-09-15 21:41:51
5. i want to know how to read a file inside zipfile.<
View Tutorial By: iman at 2009-11-26 12:50:21
6. This works great.. :)
thanks a lot
View Tutorial By: Ravi at 2010-07-03 23:34:46
7. Nice Script,
It really works for me and sav
View Tutorial By: Avi at 2010-09-11 02:25:22
8. Really just brilliant...thanks
View Tutorial By: Ash Singh at 2011-02-21 04:47:20
9. great thanks a lottttttttttttttttt
View Tutorial By: reshma at 2011-06-13 04:35:44
10. thank for this
View Tutorial By: ankit at 2011-07-15 06:48:21
11. Thank you very much for this tip.
View Tutorial By: Lise at 2011-07-15 13:27:23
12. i want excel file upload and stored into database
View Tutorial By: excel file upload and genreat pdf in php at 2011-07-25 08:24:50
13. Great tip... I am currently using it on my site -
View Tutorial By: Admin at 2011-08-30 12:13:03
14. is there any way to get the coloumn names of csv f
View Tutorial By: martin at 2011-10-16 09:16:22
15. Great!!!!!!!!!!!!!!!!!!!!
View Tutorial By: Raghbendra Nayak at 2012-03-28 12:54:35
16. very usefull i have found simple and nice code her
View Tutorial By: sourabh at 2013-01-31 06:47:58
17. excellent, thanks a lot..............
View Tutorial By: yellareddy at 2013-03-12 06:56:12
18. I must say the blog post is just useful for everyo
View Tutorial By: Intraday tips at 2013-12-03 08:43:29