COM372: Notes, Chapter 2
Storing and Retrieving Data
pages 59-80
Storing information to a file: There are several ways to store information for later use. You can store it in the client computer as a cookie (chapter 23), store it for a few minutes on the server as a session variable (also chapter 23), or write it to a database for long-term storage. An alternative, if there isn't much information and you aren't too concerned about security, is to write it to a simple text file. That is what this chapter is about.
Flat files: Flat files have none of the complexities of database files. Usually, flat file means text file. Using flat files means that you open (or create), read (or write), and close the file. Opening is done with the fopen() function. This requires you to specify whether you are opening to read, to write, or both; if you are writing, you specify whether you are writing over existing content or appending to the end; and you can specify whether you are writing in text or binary. There are 4 parameters that are passed fopen(); the first two are required, the second two are not required. Required parameters for fopen are
- file directory for file to be opened. This can be given a relative address— the PHP script must begin with
$Document_Root = $_Server['Document_Root']; (page 62) - file mode
- r—Read from start of file.
- r+—Read or write from start of file.
- w—Write from start of file, deleting current contents, if any. If the file doesn't exist, try to create it.
- w+—Open for reading or writing from start of file, deleting current contents. If the file doesn't exist, try to create it.
- x—Open for writing, cautiously. If it exists, don't write and issue a warning.
- x+—Open for reading or writing, cautiously. If it exists, dont open, and issue a warning.
- a—Append, writing at the end of the existing content.
- a+—Open for appending and reading, from end of current contents, if any.
- b—Use with other mode, indicating reading or writing in binary.
- (optional) Boolean true (1) (optional)—search the include_path for the file.
- (optional) ftp / http switch (optional)—open remotely and provide a pointer to the open file.
See pages 62-66 for details on server settings and trouble-shooting.
Writing to files: The equivalent functions fwrite() or fputs() use the syntax fwrite($filepointer, $stringtowrite), indicating a file to write to and a string to be written. An optional third parameter provides a maximum to the number of characters from the string to be written (page 68). A tab-delimited (\t) output string, with records separated by newlines (\n) is $stringtowrite = $date."\t".$firstname." first name \t" .$lastname." last name \t" .$address." address \t\n";
Closing the file: Having written, close the file with fclose($filepointer).
Reading from the file: Open a file with fopen($filepointer). Read to the end of the file using the end of file test feof($filepointer). Read a line at a time, or a maximum of maxchars with $strmyline=fgets($filepointer, maxchars) (or, for example, fgets($fp, 100), which will read 99 characters); this will read until an end of line (\n) is found, or until the end of file is reached. The variation fgetss ($fp, $maxchars, str[allowable tags]) reads up to maxchars from the file $fp, stripping out any PHP or HTML tags, except for those allowed in the string of allowable tags (page 73). The variation fgetcsv ($fp, $maxchars, [, string delimiter [, string enclosure]]) breaks a delimited string into separate array elements (see chapter 3).
Reading entire files. Read the whole file using
- readfile("$DOCUMENT_ROOT/../myfolder/myfile.txt" [,int use_include_path [, resource context]); where resource context tells you if a remote file is http or ftp (see page 74)
- fpassthru($fp), where $fp = fopen ("$DOCUMENT_ROOT/../myfolder/myfile.txt");, ie., open the file first and then pass its pointer ($fp, here) to fpassthru.
- file("$DOCUMENT_ROOT/../myfolder/myfile.txt");, assigning the file to an array.
Other file functions: file_exists($DOCUMENT_ROOT/../myfolder/myfile.txt"); checks to see whether there is a file with that name. filesize ($fp) returns the number of bytes in the file. unlink($fp) deletes the file (there is no delete function). rewind() resets the position of a file pointer to the beginning of the file. ftell() reports how far into the file the pointer is, in bytes. fseek( $fp, int offset [,int whence]) sets the pile pointer at a point starting at whence and moving offset bytes into the file.
Locking the file: The file can be locked (preventing anyone else from using it) with fthe boolean lock(). This option has four values, set as flock( $fp, LOCK_OPTION), where OPTION is
- SH—reading lock, meaning it can be shared with other readers.
- EX—writing lock, giving exclusive access for writing
- UN—lock release
- NB—no blocking, meaning no one else can lock it.
Limits of flat files. Flat files are simple and independent of any additional applications such as those required to run large databases. They have, however, several major disadvantages once the information is long or complicated. Large flat files can be very slow to work with. It is hard to find particular records in a flat file, even if they are in some kind of predetermined order. Inserting a record into the middle of a file requires a lot of work. Flat files are easy to hack, meaning they create security risks. Except for situations in which a small amount of output is desired in a text form (e.g., an order to be printed and used in paper form), relational databases overcome all of the limits of flat files, and are generally preferred.