<HTML ><HEAD ><TITLE >fgets</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="fgetcsv" HREF="function.fgetcsv.html"><LINK REL="NEXT" TITLE="fgetss" HREF="function.fgetss.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.fgetcsv.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.fgetss.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.fgets" ></A >fgets</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN25401" ></A ><P > (PHP 3, PHP 4 )</P >fgets -- Gets line from file pointer</DIV ><DIV CLASS="refsect1" ><A NAME="AEN25404" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >fgets</B > ( resource handle [, int length])<BR ></BR ><P > Returns a string of up to length - 1 bytes read from the file pointed to by <TT CLASS="parameter" ><I >handle</I ></TT >. Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, the length defaults to 1k, or 1024 bytes. </P ><P > If an error occurs, returns <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><P > Common Pitfalls: </P ><P > People used to the 'C' semantics of <B CLASS="function" >fgets()</B > should note the difference in how <TT CLASS="literal" >EOF</TT > is returned. </P ><P > The file pointer must be valid, and must point 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 > A simple example follows: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN25428" ></A ><P ><B >Example 1. Reading a file line by line</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$handle = fopen ("/tmp/inputfile.txt", "r"); while (!feof ($handle)) { $buffer = fgets($handle, 4096); echo $buffer; } fclose ($handle);</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > The <TT CLASS="parameter" ><I >length</I ></TT > parameter became optional in PHP 4.2.0, if omitted, it would assume 1024 as the line length. As of PHP 4.3, omitting <TT CLASS="parameter" ><I >length</I ></TT > will keep reading from the stream until it reaches the end of the line. If the majority of the lines in the file are all larger than 8KB, it is more resource efficient for your script to specify the maximum line length. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > This function is binary safe as of PHP 4.3. Earlier versions were not binary safe. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B >If you are having problems with <TT CLASS="literal" >PHP</TT > not recognizing the line endings when reading files either on or created by a Macintosh computer, you might want to enable the <A HREF="ref.filesystem.html#ini.auto-detect-line-endings" >auto_detect_line_endings</A > run-time configuration option.</P ></BLOCKQUOTE ></DIV ><P > See also <A HREF="function.fread.html" ><B CLASS="function" >fread()</B ></A >, <A HREF="function.fopen.html" ><B CLASS="function" >fopen()</B ></A >, <A HREF="function.popen.html" ><B CLASS="function" >popen()</B ></A >, <A HREF="function.fgetc.html" ><B CLASS="function" >fgetc()</B ></A >, <A HREF="function.fsockopen.html" ><B CLASS="function" >fsockopen()</B ></A >, and <A HREF="function.socket-set-timeout.html" ><B CLASS="function" >socket_set_timeout()</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.fgetcsv.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.fgetss.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >fgetcsv</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" >fgetss</TD ></TR ></TABLE ></DIV ></BODY ></HTML >