(PHP 4, PHP 5, PHP 7, PHP 8)
fopen — Opens file or URL
fopen() binds a named resource, specified by
filename, to a stream.
filename is of the form "scheme://...", it
is assumed to be a URL and PHP will search for a protocol handler
(also known as a wrapper) for that scheme. If no wrappers for that
protocol are registered, PHP will emit a notice to help you track
potential problems in your script and then continue as though
filename specifies a regular file.
If PHP has decided that
a local file, then it will try to open a stream on that file.
The file must be accessible to PHP, so you need to ensure that
the file access permissions allow this access.
If you have enabled
restrictions may apply.
If PHP has decided that
a registered protocol, and that protocol is registered as a
network URL, PHP will check to make sure that
enabled. If it is switched off, PHP will emit a warning and
the fopen call will fail.
The list of supported protocols can be found in Supported Protocols and Wrappers. Some protocols (also referred to as
contextand/or php.ini options. Refer to the specific page for the protocol in use for a list of options which can be set. (e.g. php.ini value
user_agentused by the
On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes.
$handle = fopen("c:\\folder\\resource.txt", "r");
mode parameter specifies the type of access
you require to the stream. It may be any of the following:
||Open for reading only; place the file pointer at the beginning of the file.|
||Open for reading and writing; place the file pointer at the beginning of the file.|
||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.|
Open for reading and writing; otherwise it has the
same behavior as
||Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() has no effect, writes are always appended.|
||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. In this mode, fseek() only affects the reading position, writes are always appended.|
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
Create and open for reading and writing; otherwise it has the
same behavior as
Open the file for writing only. If the file does not exist, it is
created. If it exists, it is neither truncated (as opposed to
Open the file for reading and writing; otherwise it has the same
||Set close-on-exec flag on the opened file descriptor. Only available in PHP compiled on POSIX.1-2008 conform systems.|
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
\nas the line ending character, Windows based systems use
\r\nas the line ending characters and Macintosh based systems (Mac OS Classic) used
\ras 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
\r\nwhen 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
't'as the last character of the
The default translation mode is
'b'. You can use the
't'mode if you are working with plain-text files and you use
\nto delimit your line endings in your script, but expect your files to be readable with applications such as old versions of notepad. You should use the
'b'in all other cases.
If you specify the 't' flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with
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
modeis ignored for php://output, php://input, php://stdin, php://stdout, php://stderr and php://fd stream wrappers.
The optional third
can be set to '1' or
true if you want to search for the file in the
A context stream resource.
Returns a file pointer resource on success,
false on failure
Upon failure, an
E_WARNING is emitted.
Example #1 fopen() examples
$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:email@example.com/somefile.txt", "w");
When using SSL, Microsoft IIS
will violate the protocol by closing the connection without sending a
close_notify indicator. PHP will report this as "SSL: Fatal
Protocol Error" when you reach the end of the data. To work around this, the
value of error_reporting should be
lowered to a level that does not include warnings.
PHP can detect buggy IIS server software when you open
the stream using the
https:// wrapper and will suppress the
warning. When using fsockopen() to create an
ssl:// socket, the developer is responsible for detecting
and suppressing this warning.
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.
This function may also succeed when
filenameis a directory. If you are unsure whether
filenameis a file or a directory, you may need to use the is_dir() function before calling fopen().