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.

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

Empty: NULLNull: falseUndefined: 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.

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 loop2. You can move around your calls to fgets() so that the testing of feof() happens in a better location

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

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().