This function is useful if
you??™re interested in simply dumping an entire file to the browser:
$file = "/home/www/articles/gilmore.html";
// Output the article to the browser.
$bytes = readfile($file);
?>
Like many of PHP??™s other file I/O functions, remote files can be opened via their
URL if the configuration parameter fopen_wrappers is enabled.
CHAPTER 10 ?– WORKING WITH T HE FILE A ND OPERATING SYSTEM 297
Reading a File According to a Predefined Format
The fscanf() function offers a convenient means for parsing a resource in accordance
with a predefined format. Its prototype follows:
mixed fscanf(resource handle, string format [, string var1])
For example, suppose you want to parse the following file consisting of Social
Security numbers (SSN) (socsecurity.txt):
123-45-6789
234-56-7890
345-67-8901
The following example parses the socsecurity.txt file:
$fh = fopen("socsecurity.txt", "r");
// Parse each SSN in accordance with integer-integer-integer format
while ($user = fscanf($fh, "%d-%d-%d")) {
// Assign each SSN part to an appropriate variable
list ($part1,$part2,$part3) = $user;
printf(Part 1: %d Part 2: %d Part 3: %d
", $part1, $part2, $part3);
}
fclose($fh);
?>
With each iteration, the variables $part1, $part2, and $part3 are assigned the three
components of each SSN, respectively, and output to the browser.
Writing a String to a File
The fwrite() function outputs the contents of a string variable to the specified resource.
Pages:
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371