Reading a File One Character at a Time
The fgetc() function reads a single character from the open resource stream specified
by handle. If the EOF is encountered, a value of FALSE is returned. Its prototype
follows:
string fgetc(resource handle)
Ignoring Newline Characters
The fread() function reads length characters from the resource specified by handle.
Reading stops when the EOF is reached or when length characters have been read. Its
prototype follows:
string fread(resource handle, int length)
Note that unlike other read functions, newline characters are irrelevant when
using fread(); therefore, it??™s often convenient to read the entire file in at once using
filesize() to determine the number of characters that should be read in:
296 CHAPTER 10 ?– WORKING WITH THE FI LE AND OPERATING SYSTEM
$file = "/home/www/data/users.txt";
// Open the file for reading
$fh = fopen($file, "rt");
// Read in the entire file
$userdata = fread($fh, filesize($file));
// Close the file handle
fclose($fh);
?>
The variable $userdata now contains the contents of the users.txt file.
Reading in an Entire File
The readfile() function reads an entire file specified by filename and immediately
outputs it to the output buffer, returning the number of bytes read. Its prototype
follows:
int readfile(string filename [, int use_include_path])
Enabling the optional use_include_path parameter tells PHP to search the paths
specified by the include_path configuration parameter.
Pages:
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370