Page 192 - Open Soource Technologies 304.indd
P. 192
Unit 11: Directories and Files
The file may be opened in one of the following modes: Notes
Modes Description
r Read only. Starts at the beginning of the file.
r+ Read/Write. Starts at the beginning of the file.
w Write only. Opens and clears the contents of file; or creates a new file if it
doesn’t exist.
w+ Read/Write. Opens and clears the contents of file; or creates a new file if it
doesn’t exist.
a Append. Opens and writes to the end of the file or creates a new file if it
doesn’t exist.
a+ Read/Append. Preserves file content by writing to the end of the file.
x Write only. Creates a new file. Returns FALSE and an error if file already
exists.
x+ Read/Write. Creates a new file. Returns FALSE and an error if file already
exists.
If the fopen() function is unable to open the specified file, it returns 0 (false).
Example: The following example generates a message if the fopen() function is unable
to open the specified file:
<html>
<body>
<?php
$file=fopen(“welcome.txt”,”r”); exit (unable to open file!”);
?>
</body>
</html>
11.6 Writing Files
Now that you know how to open and close a file, let’s get on to the most useful part of file
manipulation, writing! There is really only one main function that is used to write and it’s
logically called fwrite.
11.6.1 File Open Write
Before we can write information to our test file we have to use the function fopen to open the
file for writing.
LOVELY PROFESSIONAL UNIVERSITY 187