The PHP fopen( ) and fclose( ) Functions and Modes

The PHP fopen(  ) and fclose(  ) Functions and Modes
The fopen( ) and fclose( ) functions are two of many PHP filesystem functions and are used together, along with their modes, to open and close an existing file that is part of your website. In certain cases, the fopen( ) function may also be used to generate a new file. For example, if you want a database driven online product catalog but you do not have MySQLR or another database system available. You can use a text file database. In this case, you will need to open one or more files, collect the data from the files and display the catalog information. Let’s take a look at the fopen( ) and fclose( ) code.

Basic Code

$filename = "full path/name_of_file.txt";
$handle = fopen($filename, "r") or die("Can't open file");
. . .
fclose($handle);

Example

$filename = “catalog/products.txt”;
$handle = fopen($filename, "r") or die("Can't open file");
. . . do something . . .
fclose($handle);


fopen(filename, "mode");
The fopen( ) function takes at least two arguments which are the name of the file to be opened and the mode (or reason) for opening the file.

$filename = “full path/name_of_file.txt”;
The first part of the code will assign a value to the $filename variable. This value will be the location (path) and name of the file to be open. In the example, the $filename variable is set to catalog/products.txt. The file to be opened is called products.txt and is in the catalog folder. The .txt extension indicates that this is a text file.

$handle = fopen($filename, "r") or die("Can't open file");
In the next line of code, we use a variable which we name $handle to open the file.

The mode, placed between parentheses, indicates to the program how to setup the information in the file. In our example, the mode is set to “r” which indicates the file is to be read. The mode also tells the program where to put the pointer within the file. The pointer “points to” the location where you wish to begin reading the contents of the file. To read the entire file, the pointer will be placed at the beginning of the file. The chart below contains descriptions of the most common modes.

Mode
Description
r
read only, pointer set at beginning of file
r+
read and write, pointer set at beginning of file
w
write only, pointer set at beginning of file
will overwrite all contents of existing file
will create a file if no file exists
w+
read and write, pointer set to beginning of file
will overwrite all contents of existing file
will create a file if no file exists
a
write only, pointer set at end of file
will not overwrite contents of file
places new data at end of file
will create a file if no file exists
a+
read and write, pointer set at end of file
will not overwrite contents of file
places new data at end of file
will create a file if no file exists

As you might have noticed, it is very important to use the correct mode with the fopen( ) function or you might inadvertently place the pointer at the beginning of your file and delete all the data from your existing file. Also, some modes will create a new file with the fopen( ) function, if a file of that name does not already exist.

fclose($handle);
The fclose( ) function takes one argument which is the $handle variable. This tells the program to close the file that was just opened.






This site needs an editor - click to learn more!



RSS
Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map





Content copyright © 2023 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.