PHP Tutorial: File System

By Vince Barnes

One of the most useful features of a server side language is its ability to interface with the file system on the server that hosts the website.  This feature clearly distinguishes the language from a client side language such as JavaScript.  While JavaScript is sent down to the client's browser and is then interpreted on their computer, server side languages such as PHP are interpreted and resolved on the server, and only the end result is sent down to the client's browser.  It is because of this fact that the language has the ability to interface with the files on the server.  PHP has the ability to work with both text files and sophisticated database systems.  This part of the tutorial discusses text files; we'll get into databases later.

To access a file you first have to establish a connection to that file.  This is done by opening the file with the fopen function.  To use the fopen function you provide it two parameters.  The first is the name of the file you wish to use and the second says how you want to use the file (for reading or writing, etc.)  Using the fopen function establishes a pointer to the file.  Here's an example:

<?php
$myfilepointer = fopen("myfilename.txt", "w");

?>

here, we're opening a file called "myfilename.txt" and saying that we wish to write to it.  The "w", which is what tells PHP we want to write to the file, is called the Access Mode.  Other values that can be used, and their meanings are:

r read only
r+ reading and writing
w write only & create the file if it doesn't exist
w+ reading and writing & create the file if it doesn't exist
a writing only, create if it doesn't exist, and place a file position pointer at the end of the file (appends records to an existing file)
a+ reading and writing, create if it doesn't exist and place file position pointer at the end of the file

 

These parameters have introduced the idea of a file position pointer.  As you read a file, something has to keep track of where you are in the file so that you can read the next record.  That something is the file position pointer.  This pointer gets interesting when you think about writing to the file.  If you are writing to a new file it's pretty straight forward -- you write a record, the pointer points at the end of that record and you write another record, and so on.  If, however, the file already exists and contains some records, the value of the pointer when you first open the file becomes very important!  If the file is opened with the pointer at the beginning of the file (like r+, w or w+), when you write to that file you will overwrite the existing records in the file, replacing them with your new records.  On the other hand, if you open it with the pointer pointing at the end of the file (like a or a+), you will append records to the file, adding them to the existing records.

Reading and Writing Records

Now that we have our file open, we need to read and/or write records.  This is accomplished with the fgets and fputs functions.

Continue to the next part of this Tutorial

Return to the Tutorial Series Index



Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •