Specifications
This function will return true if the file was successfully closed or false if it wasn’t. This is
generally much less likely to go wrong than opening a file in the first place, so in this case
we’ve chosen not to test it.
Reading from a File
Right now, Bob’s customers can leave their orders via the Web, but if Bob’s staff wants to look
at the orders, they’ll have to open the files themselves.
Let’s create a Web interface to let Bob’s staff read the files easily. The code for this interface is
shown in Listing 2.2.
LISTING 2.2 vieworders.php—Staff 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