<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Tests for end-of-file on a file pointer</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.fclose.html">fclose</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fflush.html">fflush</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.feof" class="refentry"> <div class="refnamediv"> <h1 class="refname">feof</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">feof</span> — <span class="dc-title">Tests for end-of-file on a file pointer</span></p> </div> <div class="refsect1 description" id="refsect1-function.feof-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>feof</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$handle</code></span> )</div> <p class="para rdfs-comment"> Tests for end-of-file on a file pointer. </p> </div> <div class="refsect1 parameters" id="refsect1-function.feof-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">handle</code></em></span> <dd> <p class="para">The file pointer must be valid, and must point to a file successfully opened by <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> or <span class="function"><a href="function.fsockopen.html" class="function">fsockopen()</a></span> (and not yet closed by <span class="function"><a href="function.fclose.html" class="function">fclose()</a></span>).</p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.feof-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> if the file pointer is at EOF or an error occurs (including socket timeout); otherwise returns <strong><code>FALSE</code></strong>. </p> </div> <div class="refsect1 notes" id="refsect1-function.feof-notes"> <h3 class="title">Notes</h3> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> If a connection opened by <span class="function"><a href="function.fsockopen.html" class="function">fsockopen()</a></span> wasn't closed by the server, <span class="function"><strong>feof()</strong></span> will hang. To workaround this, see below example: <div class="example" id="example-2357"> <p><strong>Example #1 Handling timeouts with <span class="function"><strong>feof()</strong></span></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">safe_feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, &</span><span style="color: #0000BB">$start </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$start </span><span style="color: #007700">= </span><span style="color: #0000BB">microtime</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /><br /> return </span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/* Assuming $fp is previously opened by fsockopen() */<br /><br /></span><span style="color: #0000BB">$start </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$timeout </span><span style="color: #007700">= </span><span style="color: #0000BB">ini_get</span><span style="color: #007700">(</span><span style="color: #DD0000">'default_socket_timeout'</span><span style="color: #007700">);<br /><br />while(!</span><span style="color: #0000BB">safe_feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">$start</span><span style="color: #007700">) && (</span><span style="color: #0000BB">microtime</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">) - </span><span style="color: #0000BB">$start</span><span style="color: #007700">) < </span><span style="color: #0000BB">$timeout</span><span style="color: #007700">)<br />{<br /> </span><span style="color: #FF8000">/* Handle */<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> If the passed file pointer is not valid you may get an infinite loop, because <span class="function"><strong>feof()</strong></span> fails to return <strong><code>TRUE</code></strong>. <div class="example" id="example-2358"> <p><strong>Example #2 <span class="function"><strong>feof()</strong></span> example with an invalid file pointer</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// if file can not be read or doesn't exist fopen function returns FALSE<br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= @</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"no_such_file"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// FALSE from fopen will issue warning and result in infinite loop here<br /></span><span style="color: #007700">while (!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">)) {<br />}<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.fclose.html">fclose</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fflush.html">fflush</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>