PHP is a very useful language for working with files. Although it may not be as robust as other languages, such as PERL, when it comes to parsing files, PHP still provides great power and flexibility when it comes to working with text files.
Opening a File:
Before you can use PHP to read a file, you must first open it. To open a file, you use the fopen() function.
$file_handler = fopen($file, $mode)
You must assign the function to a variable, as above. This is referred to as a file pointer. You use the file pointer to reference the open file throughout your script.
The fopen() function takes two arguments:
- $file is the name (and path, if required) of the file.
- $mode is one of a list of available modes:
1. r— Open the file as read-only. You cannot write anything to the file. Reading begins at the beginning of the file.
2. r+ — Open the file for reading and writing. Reading and writing begin at the beginning of the file. You will delete existing contents if you write to the file without first moving the internal pointer to the end of the file.
3. w— Open the file for write-only. You cannot read data from the file. Writing begins at the beginning of the file. You will again delete contents if you write to the file without first moving the pointer to the end of the file. If the file specified by the $fie argument does not exist, then PHP attempts to create the file. Make sure your permissions are set correctly!
4. w+ — As above, but the file may also be read.
5. a— This mode is the same as the 'w' mode, with the exception that the internal pointer is placed at the end of the file. Existing contents will not be overwritten unless you rewind the internal pointer to the beginning of the file.
6. a+ — As above, but the file may also be read.
Additionally, if the file is a binary file, you must include a "b" in the mode if your OS is Windows. This chapter only deals with ASCII text files, so you will not require the "b: mode in the following example. If you do need to open a binary file, an example is below:
$file_handle = fopen($filename, "rb+")
Note the differences between the "w" and "a" modes. Using "w" to open a file effectively deletes the contents of the file, while using the "a" mode retains the contents of the file and allows you to append additional data to the end of the file.
Once you have the file opened, you can then read from or write to the file.
Reading a File:
There are multiple functions available to read from a file.
You can read data from the file using the fread() function:
$file_contents = fread($file_pointer, $length_to_read);
You must assign the value of fread() to a variable, which will contain the data that has been read. The fread() function takes two arguments:
1. $file_pointer is the file pointer to which you assigned the value from the fopen() function.
2. $length is the amount of bytes that you wish to read.
When using fread() to read a file, you can read in the entire file. To do this, you need to know the byte-size of the file. You can easily find the total byte-size of a file using the filesize() function:
$bytes = filesize($filename);
The following script demonstrates how to open a short text file and display its contents:
$file = "/path/to/text.txt";
$filepointer = fopen($file, "r");
$contents = fread($filepointer, filesize($file));
" . htmlentities($contents) . "";
Closing a File:
When you have finished reading from or writing to a file that has been opened using the fopen() function, you should always close it using the fclose() function:
Closing the file allows you to clean up any memory that was used to open it and also prevents possible file corruption that sometimes (rarely) occurs when a file is left open.