|
 |
feof (PHP 3, PHP 4, PHP 5) feof -- Проверяет, достигнут ли конец файла Описаниеbool feof ( resource handle )
Функция возвращает TRUE в случае, когда дескриптор указывает на достижение конца файла
или же если произошла ошибка (включая таймаут сокета), иначе возвращает FALSE.
Внимание |
Если подключение, открытое при помощи fsockopen()
не было закрыто сервером, feof() будет ждать достижения
таймаута прежде чем вернуть TRUE. Время таймаута по умолчанию равно 60
секундам.
Вы можете использовать stream_set_timeout() для того,
чтобы изменить это значение.
|
Указатель на файл должен быть корректным и указывать
на файл, успешно открытый функциями fopen() или
fsockopen().
feof
jorge dot mendes at constante dot pt
07-Jul-2006 09:23
feof() not reliable
I used feof() in a context of retrieving a CSV file into a database.
I had the following code:
while(!feof($file_handler)){
$row = fgets($file_handler);
(...)
}
I was processing several files, and in some of them, the last row was duplicated, so I assume that feof() is not reliable.
That's how I solved the problem:
while($row = fgets($file_handler)){
(...)
}
ironoxid at libero dot it
06-Jun-2006 06:52
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 ;)
docey
12-Jan-2006 11:07
because using an invalid handle can couse seriuos trouble,
here is some very simple code that can prevent any piece
of code to runaway using an invalid handle.
function is_valid(&$fp, $type){
if(is_resource($fp)){
if(get_resource_type($fp) == $type){
return true;
}else{
return false;
}
}else{
return false;
}
}
now all you need to do to check if you got a valid handle,
if(is_valid($fp, "file")){
// your code
}else{
// your error code
}
its that simple to prevent any of your code from using an
invalid handle. ofcourse you can change the "file" line to
any resource you want to check for invalidies.
please use good coding habits, and don't assume any
varialbles, resource or objects are good. check them!!
01-Jan-2006 06:27
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.
info at derlange dot tk
06-Aug-2005 03:20
NOTE!
if your file is empty and you make the feof()-check, it will return true the first time you call it.
example:
$handle=fopen("file","r");
while(!feof($handle))
{
$line=fgets($handle);
}
$line is now an empty string ( "" )
you have to check the size of your file before, like this sample code:
if (filesize("file")>0)
{
while( !feof($handle) )
{
$zeile=fgets($handle);
}
}
31-Jul-2005 10:21
if you hit an feof() infinite loop, watch out for resultant humongous logs, they can cripple a site with hard disk usage limits or run up excess usage fees.
04-Mar-2005 09:02
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.
bruno dot moreira at brturbo dot com
12-Jan-2005 03:43
Use it with many caution. If your php.ini don't stop the script when it enter a infinity loop.
I don't think as good the use of feof with an while loop.
See the example:
$a=open("/etc/passwd");
while(!feof($a)){
echo fgets($a);
}
I used '/etc/passwd' because obviously the apache user isn't root, then cannot read '/etc/passwd'.
If in future the feof return's some different that TRUE or FALSE, this can be used with good results.
mark at newfangled dot com
04-May-2004 10:39
When a fopen() is done on a file that has permissions that are set to
not allow the current process user to read it or the file doesn't exist
it returns false. This is expected. The problem is when feof() is fed
the invalid handle it doesn't return TRUE() thus creating an infinite
loop in the following code example.
<?php
$fp = fopen( 't.txt', 'r' );
while( !feof( $fp ) )
{
print fgets( $fp );
}
fclose( $fp );
?>
feof() would return TRUE to cancel the loop and the script would end.
There would of course be warnings because of the invalid file handle,
but that is expected.
There should be better error handling on the developers part and catch
the invalid file handle, but I would expect the file functions to handle
this situation accordingly.
Johannes
12-Mar-2004 02:47
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'.
| |