Loading a File from the Drive System

Loading a File from the Drive System

It is important that you can also retrieve information from the file system. If you open a file with the "r" access modifier, you will be able to read information from the file.

Introducing the loadSonnet.php Program

The loadSonnet.php program, shown in Figure 6.6 loads up the sonnet saved by saveSonnet.php and displays it as befits the work of the Bard.

Click To expand
Figure 6.6: The file has been loaded from the drive system and prettied up a bit with some CSS tricks.

The code for the loadSonnet program follows:

<style type = "text/css">
  font-family:'Brush Script MT', script;

$fp = fopen("sonnet76.txt", "r");

//first line is title
$line = fgets($fp);
print "<center><h1>$line</h1></center>\n";

print "<center>\n";
//print rest of sonnet
while (!feof($fp)){
  $line = fgets($fp);
  print "$line <br>\n";
} // end while

print "</center>\n";




Using CSS to Beautify the Output

The best way to improve the appearance of text is with CSS styles. By setting up a simple stylesheet, I was able to very quickly improve the appearance of the sonnet without changing the actual text of the sonnet at all. Notice especially how I indicated multiple fonts in case my preferred font was not installed on the user's system.

Using the "r" Access Modifier

In order to read from a file, you must get a file pointer by opening that file for "r" access. If the file does not exist, you will get the result FALSE rather than a file pointer.


You can actually open files anywhere on the Internet for read access. If you supply a URL as a filename, you will be able to read the URL as if it were a local file. However, you cannot open URL files for output.

I opened "sonnet76.txt" with the fopen() command using the "r" access modifier, and again copied the resulting integer to the $fp file pointer variable.

Checking for the End of the File with feof()

When you are reading data from a file, your program doesn't generally know how long the file will be. The fgets() command that you will frequently use to get data from a file reads one line of the file at a time. Since you can't be sure how many lines are in a file until you read it, PHP provides a special function called feof(), which stands for file end of file (apparently named by the department of redundancy department). This function returns the value FALSE if there are any more lines of data left in the file, and TRUE when the program is at the end of the data file. Most of the time when you read data from the file, you'll use a while loop that continues as long as feof() is not true. The easiest way to set up this loop is with a statement like this:

while (!feof($fp)){

The feof() function requires a file pointer as its sole parameter.

Reading Data from the File with fgets()

The fgets() function gets one line of data from the file, returns that value as a string, and moves a special pointer to the next line of the file. Usually this function is called inside a loop that continues until feof() is true.