Reading from Files

PHP provides a number of functions for reading data from files. These enable you to read by the byte, the line, and even by the character.

Reading Lines from a File with fgets() and feof()

After you open a file for reading, you often need to access it line by line. To read a line from an open file, you can use fgets(), which requires the file resource returned from fopen() as an argument. You must also pass fgets() an integer as a second argument. The integer argument specifies the number of bytes that the function should read if it doesn't first encounter a line end or the end of the file. The fgets() function reads the file until it reaches a newline character ("\n"), the number of bytes specified in the length argument, or the end of the file.

 $line = fgets($fp, 1024); // where $fp is the file resource returned by fopen() 

Although you can read lines with fgets(), you need some way to tell when you reach the end of the file. The feof() function does this by returning true when the end of the file has been reached and false otherwise. feof() requires a file resource as its argument.

 feof($fp); // where $fp is the file resource returned by fopen() 

You now have enough information to read a file line by line, as shown in Listing 10.9.

Listing 10.9 Opening and Reading a File Line by Line
   1: <html>   2: <head>   3: <title>Listing 10.9 Opening and reading a file line by line</title>   4: </head>   5: <body>   6: <?php   7: $filename = "test.txt";   8: $fp = fopen($filename, "r") or die("Couldn't open $filename");   9: while (!feof($fp)) {  10:      $line = fgets($fp, 1024);  11:      print "$line<br>";  12: }  13: ?>  14: </body>  15: </html> 

If this code were saved to the document root of your Web server and run through your Web browser, the output would look something like Figure 10.3 (the contents of your sample text file might be different).

Figure 10.3. Output of Listing 10.9.

graphics/10fig03.gif

We call fopen() on line 8 with the name of the file that we want to read, using the or operator to ensure that script execution ends if the file cannot be read. This usually occurs if the file does not exist, or (on a Unix system) if the file's permissions don't allow the script read access to the file. The actual reading takes place in the while statement on line 9. The while statement's test expression calls feof() for each iteration, ending the loop when it returns true. In other words, the loop continues until the end of the file is reached. Within the code block, we use fgets() on line 10 to extract a line (or 1024 bytes) of the file. We assign the result to $line and print it to the browser on line 11, appending a <BR> tag for the sake of readability.

Reading Arbitrary Amounts of Data from a File with fread()

Rather than reading text by the line, you can choose to read a file in arbitrarily defined chunks. The fread() function accepts a file resource as an argument, as well as the number of bytes you want to read. fread() returns the amount of data you requested, unless the end of the file is reached first.

 $chunk = fread($fp, 16); 

Listing 10.10 amends our previous example so that it reads data in chunks of 16 bytes rather than by the line.

Listing 10.10 Reading a File with fread()
   1: <html>   2: <head>   3: <title>Listing 10.10 Reading a file with fread()</title>   4: </head>   5: <body>   6: <?php   7: $filename = "test.txt";   8: $fp = fopen($filename, "r") or die("Couldn't open $filename");   9: while (!feof($fp)) {  10:     $chunk = fread($fp, 16);  11:     print "$chunk<br>";  12: }  13: ?>  14: </body>  15: </html> 

If this code were saved to the document root of your Web server and run through your Web browser, the output could look something like Figure 10.4.

Figure 10.4. Output of Listing 10.10.

graphics/10fig04.gif

Although fread() enables you to define the amount of data acquired from a file, it doesn't let you decide the position from which the acquisition begins. You can set this manually with the fseek() function. fseek() enables you to change your current position within a file. It requires a file resource and an integer that represents the offset from the start of the file (in bytes) to which you want to jump:

 fseek($fp, 64); 

Listing 10.11 uses fseek() and fread() to output the second half of a file to the browser.

Listing 10.11 Moving Around a File with fseek()
   1: <html>   2: <head>   3: <title>Listing 10.11 Moving around a file with fseek()</title>   4: </head>   5: <body>   6: <?php   7: $filename = "test.txt";   8: $fp = fopen($filename, "r") or die("Couldn't open $filename");   9: $fsize = filesize($filename);  10: $halfway = (int)($fsize / 2);  11: print "Halfway point: $halfway <BR>\n";  12: fseek($fp, $halfway);  13: $chunk = fread($fp, ($fsize - $halfway));  14: print $chunk;  15: ?>  16: </body>  17: </html> 

If this code were saved to the document root of your Web server and run through your Web browser, the output could look something like Figure 10.5.

Figure 10.5. Output of Listing 10.11.

graphics/10fig05.gif

We calculate the halfway point of our file by dividing the return value of filesize() by 2 on line 10. We use this as the second argument to fseek() on line 12, jumping to the halfway point. Finally, we call fread() on line 13 to extract the second half of the file and then print the result to the browser.

Reading Characters from a File with fgetc()

fgetc() is similar to fgets() except that it returns only a single character from a file every time it is called. Because a character is always one byte in size, fgetc() doesn't require a length argument. You must simply pass it a file resource:

 $char = fgetc($fp); 

Listing 10.12 creates a loop that reads the file "test.txt" one character at a time, outputting each character to the browser on its own line.

Listing 10.12 Moving Around a File with fseek()
   1: <html>   2: <head>   3: <title>Listing 10.12</title>   4: </head>   5: <body>   6: <?php   7: $filename = "test.txt";   8: $fp = fopen($filename, "r") or die("Couldn't open $filename");   9: while (!feof($fp)) {  10:    $char = fgetc($fp);  11:    print "$char<BR>";  12: }  13: ?>  14: </body>  15: </html> 

If this code were saved to the document root of your Web server and run through your Web browser, the output could look something like Figure 10.6.

Figure 10.6. Output of Listing 10.12.

graphics/10fig06.gif



Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
ISBN: 067232489X
EAN: 2147483647
Year: 2005
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net