Keep this in mind when you need
to input or output information one line at a time. Several functions introduced
throughout the remainder of this chapter offer functionality tailored to working with
the newline character. Some of these functions include file(), fgetcsv(), and fgets().
Recognizing the End-of-File Character
Programs require a standardized means for discerning when the end of a file has
been reached. This standard is commonly referred to as the end-of-file, or EOF, character.
This is such an important concept that almost every mainstream programming
language offers a built-in function for verifying whether the parser has arrived at the
EOF. In the case of PHP, this function is feof(). The feof() function determines
whether a resource??™s EOF has been reached. It is used quite commonly in file I/O
operations. Its prototype follows:
int feof(string resource)
An example follows:
// Open a text file for reading purposes
$fh = fopen("/home/www/data/users.txt", "rt");
// While the end-of-file hasn't been reached, retrieve the next line
while (!feof($fh)) echo fgets($fh);
// Close the file
fclose($fh);
?>
Opening and Closing a File
Typically you??™ll need to create what??™s known as a handle before you can do anything with
a file??™s contents. Likewise, once you??™ve finished working with that resource, you should
destroy the handle. Two standard functions are available for such tasks, both of which
are introduced in this section.
Pages:
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361