PHP - Testing for the End of a File

Introduction

The feof() function returns true when the file pointer has reached the end of the file and returns false otherwise.

It takes one argument: the file handle to test.

feof() is useful with fread() or fgetc() in a while loop when you don't know how long the file is:

$handle = fopen("hello_world.txt" ,"r" );
$text ="" ;

while (!feof($handle)) {
    $text.= fread($handle, 3);  // Read 3 chars at a time
}

echo $text." \n" ;
fclose($handle);

Related Topic