<HTML ><HEAD ><TITLE >fgetcsv</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Filesystem functions" HREF="ref.filesystem.html"><LINK REL="PREVIOUS" TITLE="fgetc" HREF="function.fgetc.html"><LINK REL="NEXT" TITLE="fgets" HREF="function.fgets.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.fgetc.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.fgets.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.fgetcsv" ></A >fgetcsv</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN25348" ></A ><P > (PHP 3>= 3.0.8, PHP 4 )</P >fgetcsv -- Gets line from file pointer and parse for CSV fields</DIV ><DIV CLASS="refsect1" ><A NAME="AEN25351" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >fgetcsv</B > ( resource handle, int length [, string delimiter [, string enclosure]])<BR ></BR ><P > Similar to <A HREF="function.fgets.html" ><B CLASS="function" >fgets()</B ></A > except that <B CLASS="function" >fgetcsv()</B > parses the line it reads for fields in <SPAN CLASS="acronym" >CSV</SPAN > format and returns an array containing the fields read. The optional third <TT CLASS="parameter" ><I >delimiter</I ></TT > parameter defaults as a comma. The optional <TT CLASS="parameter" ><I >enclosure</I ></TT > cannot be <A HREF="language.types.null.html" ><B CLASS="type" >null</B ></A >, and is limited to one character. If <TT CLASS="parameter" ><I >enclosure</I ></TT > is more than one character, only the first character is used. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > The <TT CLASS="parameter" ><I >enclosure</I ></TT > parameter was added in PHP 4.3.0. </P ></BLOCKQUOTE ></DIV ><P > The <TT CLASS="parameter" ><I >handle</I ></TT > parameter must be a valid file pointer to a file successfully opened by <A HREF="function.fopen.html" ><B CLASS="function" >fopen()</B ></A >, <A HREF="function.popen.html" ><B CLASS="function" >popen()</B ></A >, or <A HREF="function.fsockopen.html" ><B CLASS="function" >fsockopen()</B ></A >. </P ><P > The <TT CLASS="parameter" ><I >length</I ></TT > parameter must be greater than the longest line to be found in the CSV file (allowing for trailing line-end characters). </P ><P > <B CLASS="function" >fgetcsv()</B > returns <TT CLASS="constant" ><B >FALSE</B ></TT > on error, including end of file. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > A blank line in a CSV file will be returned as an array comprising a single <A HREF="language.types.null.html" ><B CLASS="type" >null</B ></A > field, and will not be treated as an error. </P ></BLOCKQUOTE ></DIV ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN25393" ></A ><P ><B >Example 1. Read and print the entire contents of a CSV file</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $row = 1; $handle = fopen ("test.csv","r"); while ($data = fgetcsv ($handle, 1000, ",")) { $num = count ($data); print "<p> $num fields in line $row: <br>\n"; $row++; for ($c=0; $c < $num; $c++) { print $data[$c] . "<br>\n"; } } fclose ($handle); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also <A HREF="function.explode.html" ><B CLASS="function" >explode()</B ></A >, <A HREF="function.file.html" ><B CLASS="function" >file()</B ></A >, and <A HREF="function.pack.html" ><B CLASS="function" >pack()</B ></A > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.fgetc.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.fgets.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >fgetc</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.filesystem.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >fgets</TD ></TR ></TABLE ></DIV ></BODY ></HTML >