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);
}