Read mode opens a file for input, so your program can read information from the file.. You cannot write data to a file that is opened in read mode.. Write mode allows you to open a file
Trang 1g r
s o
l u
g in
e r
Examining File Access Modifiers
The final parameter in the fopen()command is an access modifier PHP supports
a number of access modifiers, which determine how your program interacts with the file Files are usually opened for these modes: reading, writing, or appending Read mode opens a file for input, so your program can read information from the file You cannot write data to a file that is opened in read mode Write mode allows you to open a file for output access If the file does not exist, PHP auto-matically creates it for you Append mode allows you to write to a file without destroying the current contents When you write to a file in append mode, all new data is added to the end of the file
You can use a file for random access, which allows a file to be open simultane-ously for input and output, but such files are often not needed in PHP The rela-tional database techniques provide the same capability with more flexibility and
a lot less work However, the other forms of file access (read, write, and output) are extremely useful, because they provide easy access to the file information
Be very careful about opening a file in write mode If you open an already existing file for write access, PHP creates a new file and overwrites and destroys the old file’s contents.
T R A P
The “r+” and “w+” modifiers are used for another form of file access, called ran-dom access, which allows simultaneous reading and writing to the same file While this is a very useful tool, I won’t spend a lot of time on it in this book The sequential-access methods in this chapter are fine for simple file storage problems; the XML and relational database functions in the remainder of this book aren’t any more difficult than the random access model and provide far more power.
Trang 2Writing to a File
already a file in the current directory, it is destroyed The $fpvariable stores the
file pointer for the text file Once this is done, you can use the fputs()function
to actually write data to the file
You might be noticing a trend here Most of the file access functions begin with the letter f: fopen(), fclose(), fputs(), fgets(), feof() This convention is inherited from the C language It can help you remember that a particular function works with files Of course, every statement in PHP that begins with f isn’t necessarily a file function (foreach is a good example), but most function names
in PHP that begin with f are file-handling commands.
tells PHP where to write the data The second parameter is the text to write out
to the file
Closing a File
the file and should close it
Drive systems are much slower than computer memory and take a long time
to spool up to speed For that reason, when a program encounters an fputs() command, it doesn’t always immediately save the data to a file on the disk.
Instead, it adds the data to a special buffer and writes the data only when a suffi-cient amount is on the buffer or the program encounters an fclose() command.
This is why it’s important to close your files If the program ends without encoun-tering an fclose() statement, PHP is supposed to automatically close the file for you, but what’s supposed to happen and what actually happens are often two very different things.
Loading a File from the Drive System
You can retrieve information from the file system If you open a file with the “r”
access modifier, you can read information from the file
Introducing the loadSonnet.php Program
T R I C K
T R I C K
189
Trang 3The code for the loadSonnetprogram follows:
<html>
<head>
<title>LoadSonnet</title>
<style type = “text/css”>
body{
background-color:darkred;
color:white;
font-family:’Brush Script MT’, script; font-size:20pt
}
</style>
</head>
<body>
<?
$fp = fopen(“sonnet76.txt”, “r”);
//first line is title
$line = fgets($fp);
print “<center><h1>$line</h1></center>\n”; print “<center>\n”;
190
g r
s o
l u
g in
e r
FIGURE 6.6
The file has been
loaded from the
drive system and
prettied up a bit
with some
cascading style
sheets (CSS) tricks.
Trang 4//print rest of sonnet
while (!feof($fp)){
$line = fgets($fp);
print “$line <br>\n”;
} // end while
print “</center>\n”;
fclose($fp);
?>
</body>
</html>
Beautifying Output with CSS
CSS styles are the best way to improve text appearance By setting up a simple
style sheet, I very quickly improve the sonnet’s appearance without changing the
text 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
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 get the result FALSErather than a file pointer
You can open files anywhere on the Internet for read access If you supply a URL as a filename you can read the URL as if it were a local file However, you can-not open URL files for output.
I opened sonnet76.txtwith the fopen()command using the “r”access modifier
and again copied the resulting integer to the $fpfile 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 the
file length The fgets()command, which gets 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)
T R I C K
191
Trang 5This function returns the value FALSEif any more lines of data are left in the file.
It returns TRUEwhen the program is at the end of the data file Most of the time when you read file data, you use a whileloop 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)){
Reading Data from the File with fgets()
string, and moves a special pointer to the next line of the file Usually this func-tion is called inside a loop that continues until feof()is TRUE
Reading a File into an Array
It is often useful to work with a file by loading it into an array in memory Fre-quently you find yourself doing some operation on each array line PHP provides
a couple of features that simplify this type of operation The cartoonifier.php
program demonstrates one way of manipulating an entire file without using a file pointer
Introducing the cartoonifier.php Program
weighty use of advanced server technology
This program loads the entire sonnet into an array, steps through each line, and con-verts it to a unique cartoon dialect by performing a search and replace operation
<html>
<head>
<title>Cartoonify</title>
</head>
<body>
<?
$fileName = “sonnet76.txt”;
$sonnet = file($fileName);
$output = “”;
192
g r
s o
l u
g in
e r