Dr. Logan | COM Courses | Syllabus | Reading Notes

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 22), store it for a few minutes on the server as a session variable (also chapter 22), 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. Parameters for fopen are

See pages 62-64 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 66). 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 71). 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

The first two options pass the file directly to standard output (i.e., they will dump it into the html stream sent back to the browser for rendering on the screen). fgetc($fp) will read from the file one character at a time. fread ($fp, intstringlength) will read snippets of intstringlength characters at a time.

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

If you lock a file, you must unlock it (flock($fp, LOCK_UN)) when you are done, so that others may use it. Locking is a common concern. It isn't well developed for writing to files, but it is very well developed for relational database.

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.