PHP Tutorial - PHP feof() Function






Definition

feof() takes a file handle and returns true if you are at the end of the file or false otherwise.

Syntax

PHP feof() Function has the following syntax.

feof(file)

Parameter

ParameterIs RequiredDescription
fileRequired.Open file to check

Return

This function returns TRUE if an error occurs, or if EOF has been reached. Otherwise it returns FALSE.





Example

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


<?PHP
$huge_file = fopen("VERY_BIG_FILE.txt", "r");
while (!feof($huge_file)) {
   print fread($huge_file, 1024);
}
fclose($huge_file);

?>




Example 2

The following code shows how to binary-safe file read.


//from   www .  jav a2 s.c  o  m

<?php
// For PHP 5 and up
$handle = fopen("http://www.java2s.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
//OR
$handle = fopen("http://www.example.com/", "rb");
$contents = '';
while (!feof($handle)) {
  $contents .= fread($handle, 8192);
}
fclose($handle);
?>