feof() - 测试文件指针是否到了文件结束的位置 - php 文件目录函数
feof()
(PHP 4, PHP 5, PHP 7)
测试文件指针是否到了文件结束的位置
说明
feof(resource $handle): bool测试文件指针是否到了文件结束的位。
参数
$handle文件指针必须是有效的,必须指向由fopen()或fsockopen()成功打开的文件(并还未由fclose()关闭)。
返回值
如果文件指针到了 EOF 或者出错时则返回TRUE
,否则返回一个错误(包括 socket 超时),其它情况则返回FALSE
。
注释
Warning如果服务器没有关闭由fsockopen()所打开的连接,feof()会一直等待直到超时。要解决这个问题可参见以下范例:
处理feof()的超时
Warning如果传递的文件指针无效可能会陷入无限循环中,因为feof()不会返回TRUE
。
使用无效文件指针的feof()例子
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 ;)
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;
Here's solution 3: AFAICS fgets() never returns an empty string, so we can also write:
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. 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. I hope this helps someone.
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.
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.
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); }
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".
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.
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: 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: And here's solution 2 (IMHO, more elegant): FYI, the eof() function in C++ works the exact same way, so this isn't just some weird PHP thing...
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.
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.
To avoid infinite loop with fgets() just use do..while statement.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)