Specifications

This function will return true if the file was successfully closed or false if it wasnt. This is
generally much less likely to go wrong than opening a file in the first place, so in this case
weve chosen not to test it.
Reading from a File
Right now, Bobs customers can leave their orders via the Web, but if Bobs staff wants to look
at the orders, theyll have to open the files themselves.
Lets create a Web interface to let Bobs staff read the files easily. The code for this interface is
shown in Listing 2.2.
LISTING 2.2 vieworders.phpStaff Interface to the Orders File
<html>
<head>
<title>Bob’s Auto Parts - Customer Orders</title>
</head>
<body>
<h1>Bob’s Auto Parts</h1>
<h2>Customer Orders</h2>
<?
@ $fp = fopen(“$DOCUMENT_ROOT/../orders/orders.txt”, “r”);
if (!$fp)
{
echo “<p><strong>No orders pending.”
.”Please try again later.</strong></p></body></html>”;
exit;
}
while (!feof($fp))
{
$order= fgets($fp, 100);
echo $order.”<br>”;
}
fclose($fp);
?>
</body>
</html>
This script follows the sequence we talked about earlier: Open the file, read from the file, close
the file. The output from this script using the data file from Listing 2.1 is shown in Figure 2.4.
Storing and Retrieving Data
C
HAPTER 2
2
STORING AND
RETRIEVING DATA
59
04 7842 CH02 3/6/01 3:37 PM Page 59