52
Chapter 2 Storing and Retrieving Data
This is very simple to do, but also pretty limiting, as we’ll see later in this chapter. If
you’re dealing with information of any reasonable volume, you’ll probably want to use a
database instead. However, flat files have their uses and there are some situations when
you’ll need to know how to use them.
Writing to and reading from files in PHP is virtually identical to the way it’s done in
C. If you’ve done any C programming or UNIX shell scripting, this will all seem pretty
familiar to you.
Storing and Retrieving Bob’s Orders
In this chapter, we’ll use a slightly modified version of the order form we looked at in
the last chapter.We’ll begin with this form and the PHP code we wrote to process the
order data.
Note
The HTML and PHP scripts used in this chapter can be found in the chapter2/ folder of this book’s
CD-ROM.
We ’ve modified the form to include a quick way to obtain the customer’s shipping
address.You can see this form in Figure 2.1.
Figure 2.1 This version of the order form gets the
customer’s shipping address.
The form field for the shipping address is called address.This gives us a variable we can
access as $address when we process the form in PHP, if we have register_globals on, or
04 525x ch02 1/24/03 3:38 PM Page 52
53
Opening a File
as $_POST['address'] or $_GET['address'] if register_globals is off (see Chapter 1,
“PHP Crash Course,” for details).
We’ll write each order that comes in to the same file.Then we’ll construct a Web
interface for Bob’s staff to view the orders that have been received.
Overview of File Processing
There are three steps to writing data to a file:
1. Open the file. If the file doesn’t already exist, it will need to be created.
2. Write the data to the file.
3. Close the file.
Similarly, there are three steps to reading data from a file:
1. Open the file. If the file can’t be opened (for example, if it doesn’t exist), we need
to recognize this and exit gracefully.
2. Read data from the file.
3. Close the file.
When you want to read data from a file, you have choices about how much of the file to
read at a time.We’ll look at each of those choices in detail.
For now, we’ll start at the beginning by opening a file.
Opening a File
To open a file in PHP, we use the fopen() function.When we open the file, we need to
specify how we intend to use it.This is known as the file mode.
File Modes
The operating system on the server needs to know what you want to do with a file that
you are opening. It needs to know if the file can be opened by another script while you
have it open, and to work out if you (the owner of the script) have permission to use it
in that way. Essentially, file modes give the operating system a mechanism to determine
how to handle access requests from other people or scripts and a method to check that
you have access and permission to this particular file.
There are three choices you need to make when opening a file:
1. You might want to open a file for reading only, for writing only, or for both read-
ing and writing.
2. If writing to a file, you might want to overwrite any existing contents of a file or
to append new data to the end of the file.
3. If you are trying to write to a file on a system that differentiates between binary
and text files, you might want to specify this.
The fopen() function supports combinations of these three options.
04 525x ch02 1/24/03 3:38 PM Page 53
54
Chapter 2 Storing and Retrieving Data
Using fopen() to Open a File
Let’s assume that we want to write a customer order to Bob’s order file.You can open
this file for writing with the following:
$fp = fopen("$DOCUMENT_ROOT/ /orders/orders.txt", "w");
When fopen is called, it expects two or three parameters. Usually you’ll use two, as
shown in this code line.
The first parameter should be the file you want to open.You can specify a path to this
file as we’ve done in the previous code—our orders.txt file is in the orders directory.
We ’ve used the PHP built-in variable $HTTP_SERVER_VARS['DOCUMENT_ROOT'] but, as
with the cumbersome full names for form variables, we have assigned it a shorter name.
This variable points at the base of the document tree on your Web server.We’ve used
the
" " to mean “the parent directory of the document root directory.”This directory
is outside the document tree, for security reasons.We do not want this file to be Web
accessible except through the interface that we provide.This path is called a relative path
as it describes a position in the file system relative to the document root.
As with the short names we are giving form variables, unless register_globals is turned
on, we need the following line at the start of our script
$DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
to copy the contents of the long style variable to the short style name.
In the same way as there are different ways to access form data, there are different
ways to access the predefined server variables. Depending on your server setup you can
get at the document root through:
n
$DOCUMENT_ROOT
n
$_SERVER['DOCUMENT_ROOT']
n
$HTTP_SERVER_VARS['DOCUMENT_ROOT']
As with form data, the first style, which we have been calling short style, is only auto-
matically available if you have register_globals turned on.The second style (medium
style) cannot be turned off, but is only available in PHP 4.1 and later versions.The long
style is available on all systems, but this style is deprecated, so may not be around forever.
You could also specify an absolute path to the file.This is the path from the root
directory (
/ on a UNIX system and typically C:\ on a Windows system). On our UNIX
server, this would be /home/book/orders.The problem with doing this is that, particu-
larly if you are hosting your site on somebody else’s server, the absolute path might
change.We once learned this the hard way after having to change absolute paths in a
large number of scripts when the systems administrators decided to change the directory
structure without notice.
04 525x ch02 1/24/03 3:38 PM Page 54
55
Opening a File
If no path is specified, the file will be created or looked for in the same directory as
the script itself.This will be different if you are running PHP through some kind of CGI
wrapper and will depend on your server configuration.
In a UNIX environment, the slashes in directories will be forward slashes (/). If you
are using a Windows platform, you can use forward or backslashes. If you use back slash-
es, they must be escaped (marked as a special character) for fopen to understand them
properly.To escape a character, you simply add an additional backslash in front of it, as
shown in the following:
$fp = fopen("$DOCUMENT_ROOT\\ \\orders\\orders.txt", 'w');
Ve ry few people use backslashes in paths within PHP as it means your code will only
work on Windows. If you use forward slashes you can often move your code between
Windows and UNIX machines without alteration.
The second parameter of fopen() is the file mode, which should be a string.This
specifies what you want to do with the file. In this case, we are passing 'w' to
fopen()—this means open the file for writing. A summary of file modes is shown in
Table 2.1.
Table 2.1 Summary of File Modes for fopen
Mode Meaning
r Read mode—Open the file for reading, beginning from the start of the
file.
r+ Read mode—Open the file for reading and writing, beginning from the
start of the file.
w Write mode—Open the file for writing, beginning from the start of the
file. If the file already exists, delete the existing contents. If it does not
exist, try and create it.
w+ Write mode—Open the file for writing and reading, beginning from
the start of the file. If the file already exists, delete the existing contents.
If it does not exist, try and create it.
a Append mode—Open the file for appending (writing) only, starting
from the end of the existing contents, if any. If it does not exist, try and
create it.
a+ Append mode—Open the file for appending (writing) and reading,
starting from the end of the existing contents, if any. If it does not exist,
try and create it.
b Binary mode—Used in conjunction with one of the other modes.You might
want to use this if your file system differentiates between binary and text files.
Windows systems differentiate; UNIX systems do not.
The file mode to use in our example depends on how the system will be used.We have
used 'w', which will only allow one order to be stored in the file. Each time a new
04 525x ch02 1/24/03 3:38 PM Page 55
56
Chapter 2 Storing and Retrieving Data
order is taken, it will overwrite the previous order.This is probably not very sensible, so
we are better off specifying append mode:
$fp = fopen("$DOCUMENT_ROOT/ /orders/orders.txt", 'a');
The fopen() function has an optional third parameter.You can use it if you want to
search the include_path (set in your PHP configuration—see Appendix A,“Installing
PHP 4 and MySQL”) for a file. If you want to do this, set this parameter to 1. If you tell
PHP to search the include_path,you do not need to provide a directory name or path:
$fp = fopen('orders.txt', 'a', 1);
If fopen() opens the file successfully, a pointer to the file is returned and should be
stored in a variable, in this case $fp.You will use this variable to access the file when you
actually want to read from or write to it.
Opening Files Through FTP or HTTP
As well as opening local files for reading and writing, you can open files via FTP and
HTTP using fopen().
If the filename you use begins with ftp://,a passive mode FTP connection will be
opened to the server you specify and a pointer to the start of the file will be returned.
If the filename you use begins with http://, an HTTP connection will be opened to
the server you specify and a pointer to the response will be returned.When using HTTP
mode, you must specify trailing slashes on directory names, as shown in the following:
/>not
When you specify the latter form of address (without the slash), a Web server will nor-
mally use an HTTP redirect to send you to the first address (with the slash).Try it in
your browser.
Prior to PHP 4.0.5, the fopen() function did not support HTTP redirects, so you
must specify URLs that refer to directories with a trailing slash.
As of PHP 4.3.0 you can also open files over SSL as long as you have compiled or
enabled support for OpenSSL and you begin the name of the file with https://.
Remember that the domain names in your URL are not case sensitive, but the path
and filename might be.
Problems Opening Files
A common error you might make while trying to open a file is trying to open a file you
don’t have permission to read or write to. PHP will give you a warning similar to the
one shown in Figure 2.2.
04 525x ch02 1/24/03 3:38 PM Page 56