The mode parameter specifies the type of
access you require to the stream. It may be any of the following:
Table 1. A list of possible modes for fopen() using mode
mode Description
'r' Open for reading only; place the file pointer at the
beginning of the file.
'r+' Open for reading and writing; place the file pointer
at the beginning of the file.
'w' Open for writing only; place the file pointer at the
beginning of the file and truncate the file to zero length.
If the file does not exist, attempt to create it.
'w+' Open for reading and writing; place the file pointer
at the beginning of the file and truncate the file to zero
length. If the file does not exist, attempt to create it.
'a' Open for writing only; place the file pointer at the
end of the file. If the file does not exist, attempt to
create it.
'a+' Open for reading and writing; place the file pointer
at the end of the file. If the file does not exist, attempt
to create it.
'x' Create and open for writing only; place the file pointer
at the beginning of the file. If the file already exists,
the fopen() call will fail by returning FALSE and generating
an error of level E_WARNING. If the file does not exist,
attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT
flags for the underlying open(2) system call. This option
is supported in PHP 4.3.2 and later, and only works for
local files.
'x+' Create and open for reading and writing; place the
file pointer at the beginning of the file. If the file already
exists, the fopen() call will fail by returning FALSE and
generating an error of level E_WARNING. If the file does
not exist, attempt to create it. This is equivalent to specifying
O_EXCL|O_CREAT flags for the underlying open(2) system call.
This option is supported in PHP 4.3.2 and later, and only
works for local files.
Note: Different operating system families have different
line-ending conventions. When you write a text file and
want to insert a line break, you need to use the correct
line-ending character(s) for your operating system. Unix
based systems use \n as the line ending character, Windows
based systems use \r\n as the line ending characters and
Macintosh based systems use \r as the line ending character.
If you use the wrong line ending characters when writing
your files, you might find that other applications that
open those files will "look funny".
Windows offers a text-mode translation flag ('t') which
will transparently translate \n to \r\n when working with
the file. In contrast, you can also use 'b' to force binary
mode, which will not translate your data. To use these flags,
specify either 'b' or 't' as the last character of the mode
parameter.
The default translation mode depends on the SAPI and version
of PHP that you are using, so you are encouraged to always
specify the appropriate flag for portability reasons. You
should use the 't' mode if you are working with plain-text
files and you use \n to delimit your line endings in your
script, but expect your files to be readable with applications
such as notepad. You should use the 'b' in all other cases.
If you do not specify the 'b' flag when working with binary
files, you may experience strange problems with your data,
including broken image files and strange problems with \r\n
characters.
For portability, it is strongly recommended that you always
use the 'b' flag when opening files with fopen().
Again, for portability, it is also strongly recommended
that you re-write code that uses or relies upon the 't'
mode so that it uses the correct line endings and 'b' mode
instead.
As of PHP 4.3.2, the default mode is set to binary for
all platforms that distinguish between binary and text mode.
If you are having problems with your scripts after upgrading,
try using the 't' flag as a workaround until you have made
your script more portable as mentioned above.
The optional third use_include_path parameter can be set
to '1' or TRUE if you want to search for the file in the
include_path, too.
If the open fails, the function returns FALSE and an error
of level E_WARNING is generated. You may use @ to suppress
this warning.
Example 1. fopen() examples
<?php
$handle = fopen("/home/rasmus/file.txt", "r");
$handle = fopen("/home/rasmus/file.gif", "wb");
$handle = fopen("http://www.example.com/", "r");
$handle = fopen("ftp://user:password@example.com/somefile.txt",
"w");
?>
If you are experiencing problems with reading and writing
to files and you're using the server module version of PHP,
remember to make sure that the files and directories you're
using are accessible to the server process.
On the Windows platform, be careful to escape any backslashes
used in the path to the file, or use forward slashes.
<?php
$handle = fopen("c:\\data\\info.txt", "r");
?>
Warning
Some non-standard compliant webservers, such as IIS, send
data in a way that causes PHP to raise warnings. When working
with such servers you should lower your error_reporting
level not to include warnings.
Note: When safe mode is enabled, PHP checks whether the
directory in which you are about to operate has the same
UID (owner) as the script that is being executed.
See also Appendix J, fclose(), fgets(), fread(), fwrite(),
fsockopen(), file(), file_exists(), is_readable(), stream_set_timeout(),
and popen().