HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [228]
See http://us.php.net/manual/en/function.fopen.php for more detail on the fopen() function.
The variable $theMode would contain one of the values from the following list:
♦ r: Grants read-only access to the file
♦ w: Grants write access to the file
Be careful, though, because if you specify this mode (w) for the fopen() function and use the fwrite() function, you will completely overwrite anything that may have been in the file. Don’t use w if there’s anything in the file you want to keep.
♦ a: Grants the right to append text to the file. When you specify this mode for the fopen() function and use the fwrite() function, the fwrite() function appends whatever text you specify to the end of the existing file.
♦ r+ or w+: Grants read and write access to the file. I don’t talk about r+ and w+ in this book, except to say that they’re a special way of accessing the file. This special file access mode is called random access.This allows you to simultaneously read and write to the file. If you require this type of access, you probably should be using something more simple and powerful, like relational databases.
fwrite()
After you open a file with the fopen() function and assign the file connection to a variable (see the “fopen( )” section, earlier in this chapter, for more information), you can use the file in your PHP code. You can either read from the file, or you can write to the file with the fwrite() function.
Depending on what mode you specify when you opened the file with the fopen() function, the fwrite() function will either overwrite the entire contents of the file (if you used the w mode) or it will append the text you specify to the end of the file (if you used the a mode).
The fwrite() function has two required parameters you must pass to it: the connection to the file that was established by the fopen() function and the text you wish to write to the file. The fwrite() function returns the number of bytes written to the file on success and False on failure.
Here is an example of the fwrite() function (see the section “Storing data in a CSV file” later in this chapter for an example of the fwrite() function in action):
$writeResults = fwrite($fileConnection, $text);
The fwrite() function can also be written fputs(). fwrite() and fputs() both do the exact same thing. fputs() is just a different way of writing fwrite() fputs() is referred to as an alias of fwrite().
fclose()
After you finish working with the file, closing the file connection is important.
To close the connection to a file you’ve been working with, you must pass the connection to the file you wish to close to the fclose() function. The fclose() function will return True if it is successful in closing the connection to the file and False if it is not successful in closing the connection to the file.
Here is an example of the fclose() function:
fclose($fileConnection);
Writing a basic text file
Often, you’ll want to do something as simple as record information from a form into a text file. Figure 6-1 illustrates a simple program that responds to a form and passes the input to a text form.
Figure 6-1: Here’s a standard form that asks for some contact information.
I didn’t reproduce the code for this form here because it’s basic XHTML. Of course, it’s available on the book’s companion CD-ROM and Web site, and I encourage you to look it over there.
I’m being attacked by robots!
The basic HTML form shown here is fine, but you’ll find that when you start putting forms on the Web, you’ll eventually get attacked by robot spam programs using your form to post (often inappropriate) content through your form.
The best solution to this is a technique called CAPTCHA, which is a mechanism for determining whether a form is submitted by a human or a computer. When you fill out forms online and have to type random words or letters from a weird image, you’re