feof

(PHP 4, PHP 5)

feofПроверяет, достигнут ли конец файла

Описание

bool feof ( resource $handle )

Проверяет, достигнут ли конец файла.

Список параметров

handle

Указатель на файл должен быть корректным и указывать на файл, успешно открытый функциями fopen() или fsockopen() (и все еще не закрытым функцией fclose()).

Возвращаемые значения

Возвращает TRUE, если указатель файла указывает на EOF или произошла ошибка (в том числе таймаут сокета), иначе возвращает FALSE.

Примечания

Внимание

Если подключение, открытое при помощи fsockopen() не было закрыто сервером, feof() повиснет. Для варианта обхода этого поведения смотрите следующий пример:

Пример #1 Обработка таймаутов с функцией feof()

<?php
function safe_feof($fp, &$start NULL) {
 
$start microtime(true);

 return 
feof($fp);
}

/* Предположим, что $fp был ранее открыт с помощью fsockopen() */

$start NULL;
$timeout ini_get('default_socket_timeout');

while(!
safe_feof($fp$start) && (microtime(true) - $start) < $timeout)
{
 
/* Обработка */
}
?>

Внимание

Если передан неверный файловый указатель, то вы можете получить бесконечный цикл, так как feof() не сможет вернуть TRUE.

Пример #2 Пример feof() с неверным файловым указателем

<?php
// если файл не может быть прочтен или не существует, fopen вернет FALSE
$file = @fopen("no_such_file""r");

// FALSE от fopen вызовет предупреждение и следующий цикл станет бесконечным
while (!feof($file)) {
}

fclose($file);
?>

Коментарии

Автор:
I found feof() to be a slow function when using a non-blocking connection.

The function stream_get_meta_data() returns much quicker and has a return field 'eof'.
2004-03-12 04:47:49
http://php5.kiev.ua/manual/ru/function.feof.html
Автор:
if you're worried the file pointer is invalid, TEST IT before you go into your loop... that way it'll never be an infinite loop.
2005-03-04 11:02:02
http://php5.kiev.ua/manual/ru/function.feof.html
Автор:
if you use fseek function to pos the pointer exceed the size the file,feof still return true.so note that when you use feof as the condition of while loop.
2006-01-01 20:27:10
http://php5.kiev.ua/manual/ru/function.feof.html
you  can avoid the infinite loop and filling the error logs 
by an simple if statement 
Here is the example

    $handle = fopen("http://xml.weather.yahoo.com/forecastrss?p=AYXX0008&u=f", "r");
    $xml = "";
    if ($handle)
    {
       while (!feof($handle))
       {
           $xml .= fread($handle, 128);
       }
        fclose($handle);
    }
2006-06-03 08:58:55
http://php5.kiev.ua/manual/ru/function.feof.html
I really thought that the feof() was TRUE when the logical file pointer is a EOF.
but no ! 
we need to read and get an empty record before the eof() reports TRUE.

So

$fp = fopen('test.bin','rb');
while(!feof($fp)) {
  $c = fgetc($fp);
  // ... do something with $c 
  echo ftell($fp), ",";
}
echo 'EOF!';

prints for two time the last byte position.
If our file length is 5 byte this code prints 

0,1,2,3,4,5,5,EOF!

Because of this, you have to do another check to verify if fgetc really reads another byte (to prevent error on "do something with $c" ^_^).

To prevent errors you have to use this code

$fp = fopen('test.bin','rb');
while(!feof($fp)) {
  $c = fgetc($fp);
  if($c === false) break;
  // ... do something with $c 
}

but this is the same of

$fp = fopen('test.bin','rb');
while(($c = fgetc($fp))!==false) {
  // ... do something with $c 
}

Consequently feof() is simply useless.
Before write this note I want to submit this as a php bug but one php developer said that this does not imply a bug in PHP itself (http://bugs.php.net/bug.php?id=35136&edit=2).

If this is not a bug I think that this need at least to be noticed.

Sorry for my bad english.
Bye ;)
2006-06-06 09:52:37
http://php5.kiev.ua/manual/ru/function.feof.html
Автор:
feof() is, in fact, reliable.  However, you have to use it carefully in conjunction with fgets().  A common (but incorrect) approach is to try something like this:

<?
$fp 
fopen("myfile.txt""r");
while (!
feof($fp)) {
 
$current_line fgets($fp);
 
// do stuff to the current line here
}
fclose($fp);
?>

The problem when processing plain text files is that feof() will not return true after getting the last line of input.  You need to try to get input _and fail_ before feof() returns true.  You can think of the loop above working like this:

* (merrily looping, getting lines and processing them)
* fgets used to get 2nd to last line
* line is processed
* loop back up -- feof returns false, so do the steps inside the loop
* fgets used to get last line
* line is processed
* loop back up -- since the last call to fgets worked (you got the last line), feof still returns false, so you do the steps inside the loop again
* fgets used to try to get another line (but there's nothing there!)
* your code doesn't realize this, and tries to process this non-existent line (typically by doing the same actions again)
* now when your code loops back up, feof returns true, and your loop ends

There's two ways to solve this:

1. You can put an additional test for feof() inside the loop
2. You can move around your calls to fgets() so that the testing of feof() happens in a better location

Here's solution 1:

<?
$fp 
fopen("myfile.txt""r");
while(!
feof($fp)) {
 
$current_line fgets($fp);
  if (!
feof($fp)) {
   
// process current line
 
}
}
fclose($fp);
?>

And here's solution 2 (IMHO, more elegant):

<?
$fp 
fopen("myfile.txt""r");
$current_line fgets($fp);
while (!
feof($fp)) {
 
// process current line
 
$current_line fgets($fp);
}
fclose($fp);
?>

FYI, the eof() function in C++ works the exact same way, so this isn't just some weird PHP thing...
2006-10-24 18:27:33
http://php5.kiev.ua/manual/ru/function.feof.html
Here's solution 3:

<?
$fp 
fopen("myfile.txt""r");
while ( (
$current_line fgets($fp)) !== false ) {
 
// do stuff to the current line here
}
fclose($fp);
?>

AFAICS fgets() never returns an empty string, so we can also write:

<?
$fp 
fopen("myfile.txt""r");
while ( 
$current_line fgets($fp) ) {
 
// do stuff to the current line here
}
fclose($fp);
?>
2006-11-19 10:57:32
http://php5.kiev.ua/manual/ru/function.feof.html
Автор:
To avoid infinite loop with fgets() just use do..while statement.

<?php
if ($f fopen('myfile.txt''r')) do {
   
$line fgets($f);
   
// do any stuff here...
} while (!feof($f));
fclose($f);
2007-09-19 10:03:47
http://php5.kiev.ua/manual/ru/function.feof.html
Johannes: Remember note from stream_get_meta_data page: For socket streams this member [eof] can be TRUE  even when unread_bytes  is non-zero. To determine if there is more data to be read, use feof() instead of reading this item. 

Another thing: better not rely on the "including socket timeout" part of when feof returns true. Just found program looping two days in while(!feof($fd)) fread ... with 20 seconds timeout in PHP 4.3.10.
2008-02-28 04:17:58
http://php5.kiev.ua/manual/ru/function.feof.html
Автор:
Please note that feof() used with TCP-Connections, returns false as long as the connection is open.
It even returns false when there is no data available.

BTW: Using feof() with HTTP for a single request, you should always make sure that you set the HTTP-Header "Connection" to "close" and _not_ to "keep-alive".
2010-01-02 18:59:15
http://php5.kiev.ua/manual/ru/function.feof.html
When using feof() on a TCP stream, i found the following to work (after many hours of frustration and anger):

NOTE: I used ";" to denote the end of data transmission.  This can be modified to whatever the server's end of file or in this case, end of output character is.

<?php
        $cursor 
"";
       
$inData "";

        while(
strcmp($cursor";") != 0) {
           
$cursor fgetc($sock);
           
$inData.= $cursor;
        }
       
fclose($sock);
        echo(
$inData);
?>

Since strcmp() returns 0 when the two strings are equal, it will return non zero as long as the cursor is not ";".  Using the above method will add ";" to the string, but the fix for this is simple.

<?php
        $cursor 
"";
       
$inData "";

         
$cursor fgetc($sock);
        while(
strcmp($cursor";") != 0) {
           
$inData.= $cursor;
        }
       
fclose($sock);
        echo(
$inData);
?>

I hope this helps someone.
2010-02-06 23:01:54
http://php5.kiev.ua/manual/ru/function.feof.html
Return values in the documentation are incorrectly stated. It says: 

Returns TRUE if the file pointer is at EOF or an error occurs (including socket timeout); otherwise returns FALSE.

Correct text would be more like:

Returns FALSE if no filehandle was passed; 
returns NULL if no filehandle was passed; 
returns TRUE if the file pointer is at EOF or an error occurs (including socket timeout);
otherwise returns FALSE.

As an example, running the following from the commandline:

php -r 'echo 
    "Empty: ".var_export(feof(), true)."\n".
    "Null: ".var_export(feof(NULL), true)."\n".
    "Undefined: ".var_export(feof($undef), true)."\n"
;'

This will output:

PHP Warning:  Wrong parameter count for feof() in Command line code on line 1
PHP Warning:  feof(): supplied argument is not a valid stream resource in Command line code on line 1
PHP Warning:  feof(): supplied argument is not a valid stream resource in Command line code on line 1

Empty: NULL
Null: false
Undefined: false

This can, as other commenters have reported, result in infinite loops and massive PHP error logfiles, if the file handle returned by fopen() is invalid for any reason.
2012-02-10 01:17:11
http://php5.kiev.ua/manual/ru/function.feof.html
Автор:
Don't use feof to test if you have readed all data sent by the other end of the socket. As i know it would return true only when the other end closes the connection.
2013-10-02 10:41:32
http://php5.kiev.ua/manual/ru/function.feof.html
From washington dot edu css342:
On unix/linux, every line in a file has an End-Of-Line (EOL) character and the EOF character is after the last line. On windows, each line has an EOL characters except the last line. So unix/linux file's last line is 
      stuff, EOL, EOF 
whereas windows file's last line, if the cursor is on the line, is 
      stuff, EOF 

So set up data files on windows to be the same as on unix/linux. This way, you will correctly determine eof under both unix/linux and windows. In general, you must exit all loops and all functions immediately when you are attempting to read an item that would be past the eof. 

Here is a typical set up that will work correctly. Suppose in a data file, there are multiple lines of data. In some function is the loop where you are reading and handling this data. This loop will look similar to the following.
     // infinite loop to read and handle a line of data
     for (;;) {
        $ln = fgets($fp);
        if (feof($fp)) break;

        // read the rest of the line
        // do whatever with data
     }
If you dislike infinite loops, you can accomplish this same thing using a while loop by priming the loop and reading again at the end:
    $ln = fgets($fp);
     while (!feof($fp)) {
        // read the rest of the line
        // do whatever with data

        $ln = fgets($fp);
     }
2015-07-01 20:34:44
http://php5.kiev.ua/manual/ru/function.feof.html
To avoid infinite loops and the warning :
"Warning: feof() expects parameter 1 to be resource, boolean given"

You need to check that the fopen function return the correct type.
This can be achieved very easily with gettype().
Here is an example :

$source = fopen($xml_uri, "r");
$xml = "";
                   
if(gettype($source) == "resource") { // Check here !
    while (!feof($source)) {
        $xml .= fgets($source, 4096);
    }
}

echo $xml;
2015-08-13 12:13:15
http://php5.kiev.ua/manual/ru/function.feof.html
Автор:
feof() does not test for the actual end of a file, it tests for an exceptional condition known as end-of-file. (It's based on the C function of the same name which "tests the  end-of-file  indicator  for  the  stream")

That is to say, feof() only tells you whether fread() (and friends) have run into EOF, to allow you to differentiate it from other errors. You should be testing the return value of fread() (or whatever function you're using to read), not feof().

In particular, if your filehandle is invalid (file doesn't exist / permissions issue / etc.) or fread() encounters some other error, feof will return false, and your code will be running an infinite loop processing the FALSE returned from fread().

From the (C) manpage for fread():
       fread() does not distinguish between end-of-file and error, and callers
       must use feof(3) and ferror(3) to determine which occurred.
That is the SOLE purpose for feof().
2018-07-10 22:22:54
http://php5.kiev.ua/manual/ru/function.feof.html

    Поддержать сайт на родительском проекте КГБ