<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Opens file or URL</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.fnmatch.html">fnmatch</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fpassthru.html">fpassthru</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.fopen" class="refentry"> <div class="refnamediv"> <h1 class="refname">fopen</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">fopen</span> — <span class="dc-title">Opens file or URL</span></p> </div> <div class="refsect1 description" id="refsect1-function.fopen-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><strong>fopen</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$mode</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$use_include_path</code><span class="initializer"> = false</span></span> [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></span> ]] )</div> <p class="para rdfs-comment"> <span class="function"><strong>fopen()</strong></span> binds a named resource, specified by <em><code class="parameter">filename</code></em>, to a stream. </p> </div> <div class="refsect1 parameters" id="refsect1-function.fopen-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">filename</code></em></span> <dd> <p class="para"> If <em><code class="parameter">filename</code></em> 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 <em><code class="parameter">filename</code></em> specifies a regular file. </p> <p class="para"> If PHP has decided that <em><code class="parameter">filename</code></em> specifies 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 <a href="ini.sect.safe-mode.html#ini.safe-mode" class="link">safe mode</a>, or <a href="ini.core.html#ini.open-basedir" class="link">open_basedir</a> further restrictions may apply. </p> <p class="para"> If PHP has decided that <em><code class="parameter">filename</code></em> specifies a registered protocol, and that protocol is registered as a network URL, PHP will check to make sure that <a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">allow_url_fopen</a> is enabled. If it is switched off, PHP will emit a warning and the fopen call will fail. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The list of supported protocols can be found in <a href="wrappers.html" class="xref">Supported Protocols and Wrappers</a>. Some protocols (also referred to as <em>wrappers</em>) support <em>context</em> and/or <var class="filename">php.ini</var> options. Refer to the specific page for the protocol in use for a list of options which can be set. (e.g. <var class="filename">php.ini</var> value <em>user_agent</em> used by the <em>http</em> wrapper). </p> </p></blockquote> <p class="para"> On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes. <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"c:\\folder\\resource.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">mode</code></em></span> <dd> <p class="para"> The <em><code class="parameter">mode</code></em> parameter specifies the type of access you require to the stream. It may be any of the following: <table class="doctable table"> <caption><strong> A list of possible modes for <span class="function"><strong>fopen()</strong></span> using <em><code class="parameter">mode</code></em> </strong></caption> <thead> <tr> <th><em><code class="parameter">mode</code></em></th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td><em>'r'</em></td> <td> Open for reading only; place the file pointer at the beginning of the file. </td> </tr> <tr> <td><em>'r+'</em></td> <td> Open for reading and writing; place the file pointer at the beginning of the file. </td> </tr> <tr> <td><em>'w'</em></td> <td> 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. </td> </tr> <tr> <td><em>'w+'</em></td> <td> 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. </td> </tr> <tr> <td><em>'a'</em></td> <td> Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. </td> </tr> <tr> <td><em>'a+'</em></td> <td> 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. </td> </tr> <tr> <td><em>'x'</em></td> <td> Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the <span class="function"><strong>fopen()</strong></span> call will fail by returning <strong><code>FALSE</code></strong> and generating an error of level <strong><code>E_WARNING</code></strong>. If the file does not exist, attempt to create it. This is equivalent to specifying <em>O_EXCL|O_CREAT</em> flags for the underlying <em>open(2)</em> system call. </td> </tr> <tr> <td><em>'x+'</em></td> <td> Create and open for reading and writing; otherwise it has the same behavior as <em>'x'</em>. </td> </tr> <tr> <td><em>'c'</em></td> <td> 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 <em>'w'</em>), nor the call to this function fails (as is the case with <em>'x'</em>). The file pointer is positioned on the beginning of the file. This may be useful if it's desired to get an advisory lock (see <span class="function"><a href="function.flock.html" class="function">flock()</a></span>) before attempting to modify the file, as using <em>'w'</em> could truncate the file before the lock was obtained (if truncation is desired, <span class="function"><a href="function.ftruncate.html" class="function">ftruncate()</a></span> can be used after the lock is requested). </td> </tr> <tr> <td><em>'c+'</em></td> <td> Open the file for reading and writing; otherwise it has the same behavior as <em>'c'</em>. </td> </tr> </tbody> </table> </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> 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 <em>\n</em> as the line ending character, Windows based systems use <em>\r\n</em> as the line ending characters and Macintosh based systems use <em>\r</em> as the line ending character. </p> <p class="para"> 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". </p> <p class="para"> Windows offers a text-mode translation flag (<em>'t'</em>) which will transparently translate <em>\n</em> to <em>\r\n</em> when working with the file. In contrast, you can also use <em>'b'</em> to force binary mode, which will not translate your data. To use these flags, specify either <em>'b'</em> or <em>'t'</em> as the last character of the <em><code class="parameter">mode</code></em> parameter. </p> <p class="para"> 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 <em>'t'</em> mode if you are working with plain-text files and you use <em>\n</em> to delimit your line endings in your script, but expect your files to be readable with applications such as notepad. You should use the <em>'b'</em> in all other cases. </p> <p class="para"> 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 <em>\r\n</em> characters. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> For portability, it is strongly recommended that you always use the 'b' flag when opening files with <span class="function"><strong>fopen()</strong></span>. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Again, for portability, it is also strongly recommended that you re-write code that uses or relies upon the <em>'t'</em> mode so that it uses the correct line endings and <em>'b'</em> mode instead. </p> </p></blockquote> </dd> </dt> <dt> <span class="term"><em><code class="parameter">use_include_path</code></em></span> <dd> <p class="para"> The optional third <em><code class="parameter">use_include_path</code></em> parameter can be set to '1' or <strong><code>TRUE</code></strong> if you want to search for the file in the <a href="ini.core.html#ini.include-path" class="link">include_path</a>, too. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">context</code></em></span> <dd> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">Context support was added with PHP 5.0.0. For a description of <em>contexts</em>, refer to <a href="book.stream.html" class="xref">Streams</a>.</span></p></blockquote> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.fopen-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns a file pointer resource on success, or <strong><code>FALSE</code></strong> on error. </p> </div> <div class="refsect1 errors" id="refsect1-function.fopen-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> If the open fails, an error of level <strong><code>E_WARNING</code></strong> is generated. You may use <a href="language.operators.errorcontrol.html" class="link">@</a> to suppress this warning. </p> </div> <div class="refsect1 changelog" id="refsect1-function.fopen-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.2.6</td> <td> The <em>'c'</em> and <em>'c+'</em> options were added </td> </tr> <tr> <td>4.3.2</td> <td> 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 <em>'t'</em> flag as a workaround until you have made your script more portable as mentioned before </td> </tr> <tr> <td>4.3.2</td> <td> The <em>'x'</em> and <em>'x+'</em> options were added </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.fopen-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2385"> <p><strong>Example #1 <span class="function"><strong>fopen()</strong></span> examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"/home/rasmus/file.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"/home/rasmus/file.gif"</span><span style="color: #007700">, </span><span style="color: #DD0000">"wb"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.com/"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"ftp://user:password@example.com/somefile.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.fopen-notes"> <h3 class="title">Notes</h3> <div class="warning"><strong class="warning">Warning</strong><p class="para">When using SSL, Microsoft IIS will violate the protocol by closing the connection without sending a <em>close_notify</em> 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 <a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> should be lowered to a level that does not include warnings. PHP 4.3.7 and higher can detect buggy IIS server software when you open the stream using the <em>https://</em> wrapper and will suppress the warning. When using <span class="function"><a href="function.fsockopen.html" class="function">fsockopen()</a></span> to create an <em>ssl://</em> socket, the developer is responsible for detecting and suppressing this warning.</p></div> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">When <a href="features.safe-mode.html" class="link">safe mode</a> is enabled, PHP checks whether the directory in which the script is operating has the same UID (owner) as the script that is being executed.</span></p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> 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. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> This function may also succeed when <em><code class="parameter">filename</code></em> is a directory. If you are unsure whether <em><code class="parameter">filename</code></em> is a file or a directory, you may need to use the <span class="function"><a href="function.is-dir.html" class="function">is_dir()</a></span> function before calling <span class="function"><strong>fopen()</strong></span>. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.fopen-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="wrappers.html" class="xref">Supported Protocols and Wrappers</a></li> <li class="member"> <span class="function"><a href="function.fclose.html" class="function" rel="rdfs-seeAlso">fclose()</a> - Closes an open file pointer</span></li> <li class="member"> <span class="function"><a href="function.fgets.html" class="function" rel="rdfs-seeAlso">fgets()</a> - Gets line from file pointer</span></li> <li class="member"> <span class="function"><a href="function.fread.html" class="function" rel="rdfs-seeAlso">fread()</a> - Binary-safe file read</span></li> <li class="member"> <span class="function"><a href="function.fwrite.html" class="function" rel="rdfs-seeAlso">fwrite()</a> - Binary-safe file write</span></li> <li class="member"> <span class="function"><a href="function.fsockopen.html" class="function" rel="rdfs-seeAlso">fsockopen()</a> - Open Internet or Unix domain socket connection</span></li> <li class="member"> <span class="function"><a href="function.file.html" class="function" rel="rdfs-seeAlso">file()</a> - Reads entire file into an array</span></li> <li class="member"> <span class="function"><a href="function.file-exists.html" class="function" rel="rdfs-seeAlso">file_exists()</a> - Checks whether a file or directory exists</span></li> <li class="member"> <span class="function"><a href="function.is-readable.html" class="function" rel="rdfs-seeAlso">is_readable()</a> - Tells whether a file exists and is readable</span></li> <li class="member"> <span class="function"><a href="function.stream-set-timeout.html" class="function" rel="rdfs-seeAlso">stream_set_timeout()</a> - Set timeout period on a stream</span></li> <li class="member"> <span class="function"><a href="function.popen.html" class="function" rel="rdfs-seeAlso">popen()</a> - Opens process file pointer</span></li> <li class="member"> <span class="function"><a href="function.stream-context-create.html" class="function" rel="rdfs-seeAlso">stream_context_create()</a> - Creates a stream context</span></li> <li class="member"> <span class="function"><a href="function.umask.html" class="function" rel="rdfs-seeAlso">umask()</a> - Changes the current umask</span></li> <li class="member"><a href="class.splfileobject.html" class="classname">SplFileObject</a></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.fnmatch.html">fnmatch</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fpassthru.html">fpassthru</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>