logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
Painting
Heart Disease
Horror Literature
Dating
Hiking & Backpacking
SF/Fantasy Books
Healthy Foods


dailyclick
All times in EST

Full Schedule
g
g PHP Site

BellaOnline's PHP Editor

g

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

Guest Author - Diane Cipollo

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!

Add The+PHP+fopen%28++%29+and+fclose%28++%29+Functions+and+Modes to Twitter Add The+PHP+fopen%28++%29+and+fclose%28++%29+Functions+and+Modes to Facebook Add The+PHP+fopen%28++%29+and+fclose%28++%29+Functions+and+Modes to MySpace Add The+PHP+fopen%28++%29+and+fclose%28++%29+Functions+and+Modes to Del.icio.us Digg The+PHP+fopen%28++%29+and+fclose%28++%29+Functions+and+Modes Add The+PHP+fopen%28++%29+and+fclose%28++%29+Functions+and+Modes to Yahoo My Web Add The+PHP+fopen%28++%29+and+fclose%28++%29+Functions+and+Modes to Google Bookmarks Add The+PHP+fopen%28++%29+and+fclose%28++%29+Functions+and+Modes to Stumbleupon Add The+PHP+fopen%28++%29+and+fclose%28++%29+Functions+and+Modes to Reddit




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


For FREE email updates, subscribe to the PHP Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2013 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.

g


g features
Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2013 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor