Opening Files
PHP provides access to files on both Windows and Unix based operating systems for the purpose of reading, writing, or appending content. This section describes how to use PHP to open files on Windows systems.
PHP includes the fopen() and fclose() functions for working with files. Both functions are defined below:
- fopen (filename, mode) - function used to open a file. The function requires a filename and mode. It returns a file pointer which provides information about the file and is used as a reference.
- fclose (resource $handle) - function used to close a file. The function requires the file pointer (i.e., $handle) created when the file was opened using the fopen() function. Returns TRUE on success or FALSE on failure.
The file name is the full path to the file that you want to create or open. The path can be a relative path to the file (e.g., "/MyDocuments/PHP/myfile.txt") or an absolute path to the file (e.g., "E:/MyFiles/PHP/myfile.txt"). For each directory specified, you must have the proper NTFS permissions to create, modify, and delete files.
The mode can be any of the following:
Modes Used with fopen()
Mode | Usage |
r | Opens an existing file for the purpose of reading data from it. The file pointer is placed at the beginning of the file. |
r+ | Opens an existing file for the purpose of reading or writing data. The file pointer is placed at the beginning of the file. |
w | Opens an file for writing. If the file does not exist, it is created. If the file exists, the file pointer is placed at the beginning of the file and the function deletes all existing content. |
w+ | Opens a file for reading and writing. If the file does not exist, it is created. If the file exists, the the file pointer is palced at the beginning of the file and the function deletes all existing content. |
a | Opens a file for writing. If the file does not exist, it is created. If the file exists, the file pointer is placed at the end of the file. |
a+ | Opens a file for reading and writing. If the file does not exist, it is created. If the file exists, the file pointer is placed at the the end of the file. |
The following example shows how to open file for reading:
file_process.php
<?php
$filename = "absolute or relative file path/myfile.txt";
$newfile = fopen($filename, "w+");
//code to read or write data to the file goes here
fclose($newfile);
?>
The first step is to create a variable to hold the full path to the file that will be opened:
$filename = "absolute or relative file path/myfile.txt";
The path to the text file, myfile.txt, is stored in the variable called filename. Next, a file pointer, called $newfile, is created and used with the fopen() function to open the file specified in the previous section. A file pointer is a PHP reference variable used to refer to the just-opened file:
$newfile = fopen($filename, "w+");
The file pointer will be used later to read and write content to the opened file.
In some instances, the fopen() function may not be able to successfully open a file as a result of an invalid file path, security permissions, or other unforseen issues. For these reasons, it is recommended that a special PHP function be used to gracefully handle such errors. These functions can be used in combination with the PHP error control operator "@" discussed in section 8-2 to terminate the script, supress PHP generated errors, and display a more user-friendly message. The PHP error control functions are described below:
- exit(error message) - terminates the current script and outputs the error message supplied to the function.
- die(error message) - an alias of the exit() function.
The follow script demonstrates the use of the error control functions:
fileprocess.php
<?php
$filename = "absolute or relative file path/myfile.txt";
$newfile = @fopen($filename, "w+") or exit("Could not open or create the file");
//code to read or write data to the file goes here
fclose($newfile);
?>
If the file cannot be opened, the exit() function displays the message "Could not open or create the file" and the script terminates.
If the file is opened successfully,content can be read from the file,written to the file, or appended to the file depending on the mode used with the fopen() function. Details on these actions will be discussed in the next sections. After all file processing is complete, the fclose() function is used to close the open file.