Reading Characters: fgetcHere's another reading functionthe fgetc function, which lets you read a single character from an open file. You can see how this works in Example 7-7, phpfgetc.php, where we're reading the contents of file.txt character by character and echoing it in a browser window. Example 7-7. Accessing base class methods, phpfgetc.php<HTML> <HEAD> <TITLE>Reading a file with fgetc</TITLE> </HEAD> <BODY> <CENTER> <H1>Reading a file with fgetc</H1> <?php $handle = fopen("file.txt", "r"); while ($char = fgetc($handle)) { if($char == "\n"){ $char = "<BR>"; } echo "$char"; } fclose($handle); ?> </CENTER> </BODY> </HTML> The results appear in Figure 7-7 (note that the code changes line endings to <BR>). Figure 7-7. Reading from a file using fgetc. |