<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.filesystem.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.feof.php',
    1 => 'feof',
    2 => 'Tests for end-of-file on a file pointer',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Filesystem Functions',
  ),
  'prev' => 
  array (
    0 => 'function.fdatasync.php',
    1 => 'fdatasync',
  ),
  'next' => 
  array (
    0 => 'function.fflush.php',
    1 => 'fflush',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/filesystem/functions/feof.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.feof" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">feof</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">feof</span> &mdash; <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="methodname"><strong>feof</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> <code class="parameter">$stream</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></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><code class="parameter">stream</code></dt>
     <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.php" class="function">fopen()</a></span> or
<span class="function"><a href="function.fsockopen.php" class="function">fsockopen()</a></span> (and not yet closed by
<span class="function"><a href="function.fclose.php" class="function">fclose()</a></span>).</p>
     </dd>
    
   </dl>
  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.feof-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> if the file pointer is at EOF or an error occurs
   (including socket timeout); otherwise returns <strong><code><a href="reserved.constants.php#constant.false">false</a></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.php" class="function">fsockopen()</a></span> wasn&#039;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-1">
     <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">&lt;?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">, &amp;</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">) &amp;&amp; (</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">) &lt; </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">?&gt;</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><a href="reserved.constants.php#constant.true">true</a></code></strong>.
    <div class="example" id="example-2">
     <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">&lt;?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">?&gt;</span></span></code></div>
     </div>

    </div>
   </p>
  </div>
 </div>

 
</div><?php manual_footer($setup); ?>