Chapter 6: Writing and Editing

Google

Files: reading and writing data

Here and there in this book, you'll run into code that opens, reads from, writes to, appends to and closes files. You'll need at least a basic understanding of how it works so the code won't be confusing.

A file is any collection of data that's stored in a certain place on a computer's disk drives (or other storage medium such as a Zip Drive, CD, etc.) and can be located by using its name. It could be a text file, a script, a program, an image or any number of other things.

Here's a short list of things you can do with files:

  • Open a file using fopen()
  • Close a file using fclose()
  • Read from a file using fread()
  • Write to a new file using fwrite()
  • Write new data to a file, wiping out the old data
  • Add data to the end of an existing file

Before you can do anything with a file, you have to assign its name to a variable. You do that like this:

$myFile = "somefile.txt";

Now $myFile holds the name of the file. Next we need to create a file pointer (a reference to where the file actually is) and open the file using fopen(). It's fairly common to name a file pointer $fp, but you can make up any name you like. Here's a line that will open a file for reading only:

$fp = fopen($myFile,"r");

Here, our file pointer is $fp. After this line of code is processed, $fp represents the file and how it was opened. We use $fp to refer to the file, do things to it, and eventually close it. The fopen() function needed two pieces of information from us: the name of the file to open and what "mode" to open it with. File modes include read, write, append, etc.

In case you're curious about file modes already, here's a list of them and what they do:
File Mode What it Does
 
r Opens an existing file for reading
 
r+ Opens an existing file for reading and writing
 
w Opens a file for writing. If the file doesn't exist, it's created automatically. If the file does exist, you'll wipe out any existing contents and write new stuff there. Note that file creation will fail if you don't have write permission for the folder where you want to write the file.
 
w+ Opens a file for writing and reading. The rules for "w" also apply. Note that this behavior is exactly the same as what you get with "r+."
 
a Opens a file for appending. Writing begins at the end of the file, preserving any previous file contents. If the file doesn't exist, a new (empty) file is created and writing begins.
 
a+ Opens a file for reading and appending. Writing begins at the end of the file, preserving any previous file contents. If the file doesn't exist, a new (empty) file is created and writing begins.

To read data from a file, we use the fread() function. We have to specify the file pointer and how many bytes to read. Here's an example:

$data = fread($fp, 128);

Here we read 128 bytes from the file that $fp points to. But what if we don't know how many bytes are in the file? Oops! fread() needs to know! No biggie; there's a function called filesize() we can use to find out how many bytes are in the file:

$number_of_bytes = filesize($myFile);

Notice that the filesize() function wants the file name, not the file pointer. You can even use the name of the file in quotes instead of a variable if you want to. By substituting the output of filesize($myFile) for the size parameter of fread(), we can make it work without having to know the size of $myFile in advance. That's handy.

$data = fread($fp, filesize($myFile));

To write to a file, we use the fwrite() function. It works for both string and binary data. There's a similar function, fputs(), that can only be used for string data. Here's fwrite() in action:

fwrite($fp, $data);

Warning: double-quotes (") have a special meaning. They indicate the beginning and end of a character string. If the string of characters is intended to have double-quotes inside it, they have to "protected" or "escaped" by preceding them with a backslash (\). Take this sentence for example:

It's a really "hot" seller.

To save it in a variable, we have to write it like this:

$sentence = "It's a really \"hot\" seller.";

You can use an optional "length" parameter with fwrite to specify writing a certain number of bytes to the file, but there's a catch. Any "\" characters you used to protect double-quotes inside of quoted strings will not be automatically removed. Use with this caution:

fwrite($fp, $data, 128);

When you're finished with a file, you should close it. It's very simple:

fclose($fp);


Previous Page   Table of Contents   Next Page

Copyright © 2004 Steve Humphrey