PHP Function Reference

PHP feof() Function



The PHP feof() function checks if the "end-of-file" (EOF) has been reached on a file pointer.

Syntax

feof(stream)

Parameters

stream Required. Specify the file pointer to check. It must be valid, and must point to a file successfully opened by fopen() or fsockopen().

Return Value

Returns true if the file pointer is at EOF or an error occurs (including socket timeout), otherwise returns false.

Example:

Lets assume that we have a file called test.txt. This file contains following content:

This is a test file.
It contains dummy content.

In the example below, the file is opened using fopen() function. Then the file is read line by line until EOF is reached. After performing the reading operation, it is closed using fclose() function.

<?php
//open the file in read mode
$fp = fopen("test.txt", "r") or 
         die("Unable to open file!");

//reading the file line by line
//and displaying the read content
while(!feof($fp)) {
  echo fgets($fp);
}

//close the file
fclose($fp);
?>

The output of the above code will be:

This is a test file.
It contains dummy content.

❮ PHP Filesystem Reference