<HTML ><HEAD ><TITLE >fopen</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Filesystem functions" HREF="ref.filesystem.html"><LINK REL="PREVIOUS" TITLE="fnmatch" HREF="function.fnmatch.html"><LINK REL="NEXT" TITLE="fpassthru" HREF="function.fpassthru.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.fnmatch.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.fpassthru.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.fopen" ></A >fopen</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN25887" ></A ><P > (PHP 3, PHP 4 )</P >fopen -- Opens file or URL</DIV ><DIV CLASS="refsect1" ><A NAME="AEN25890" ></A ><H2 >Description</H2 >resource <B CLASS="methodname" >fopen</B > ( string filename, string mode [, int use_include_path [, resource zcontext]])<BR ></BR ><P > <B CLASS="function" >fopen()</B > binds a named resource, specified by <TT CLASS="parameter" ><I >filename</I ></TT >, to a stream. If <TT CLASS="parameter" ><I >filename</I ></TT > 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 <TT CLASS="parameter" ><I >filename</I ></TT > specifies a regular file. </P ><P > If PHP has decided that <TT CLASS="parameter" ><I >filename</I ></TT > 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="features.safe-mode.html#ini.safe-mode" >safe_mode</A >, or <A HREF="features.safe-mode.html#ini.open-basedir" >open_basedir</A > further restrictions may apply. </P ><P > If PHP has decided that <TT CLASS="parameter" ><I >filename</I ></TT > specifies a registered protocol, and that protocol is registered as a network URL, PHP will check to make sure that <A HREF="ref.filesystem.html#ini.allow-url-fopen" >allow_url_fopen</A > is enabled. If it is switched off, PHP will emit a warning and the fopen call will fail. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > The list of supported protocols can be found in <A HREF="wrappers.html" >Appendix I</A >. </P ></BLOCKQUOTE ></DIV ><P > <TT CLASS="parameter" ><I >mode</I ></TT > specifies the type of access you require to the stream. It may be any of the following: <P ></P ><UL ><LI ><P > 'r' - Open for reading only; place the file pointer at the beginning of the file. </P ></LI ><LI ><P > 'r+' - Open for reading and writing; place the file pointer at the beginning of the file. </P ></LI ><LI ><P > '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. </P ></LI ><LI ><P > '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. </P ></LI ><LI ><P > '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. </P ></LI ><LI ><P > '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. </P ></LI ></UL > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > The <TT CLASS="parameter" ><I >mode</I ></TT > may contain the letter 'b'. This is useful only on systems which differentiate between binary and text files (i.e. Windows. It's useless on Unix). If not needed, this will be ignored. You are encouraged to include the 'b' flag in order to make your scripts more portable. </P ></BLOCKQUOTE ></DIV ><P > The optional third <TT CLASS="parameter" ><I >use_include_path</I ></TT > parameter can be set to '1' or <TT CLASS="constant" ><B >TRUE</B ></TT > if you want to search for the file in the <A HREF="configuration.directives.html#ini.include-path" >include_path</A >, too. </P ><P > The optional fourth <TT CLASS="parameter" ><I >zcontext</I ></TT > is used for specifying tuning parameters and callbacks. </P ><P > If the open fails, the function returns <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN25949" ></A ><P ><B >Example 1. <B CLASS="function" >fopen()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?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"); ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 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 > On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes. <DIV CLASS="informalexample" ><A NAME="AEN25955" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $handle = fopen ("c:\\data\\info.txt", "r"); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > See also <A HREF="wrappers.html" >Appendix I</A >, <A HREF="function.fclose.html" ><B CLASS="function" >fclose()</B ></A >, <A HREF="function.fgets.html" ><B CLASS="function" >fgets()</B ></A >, <A HREF="function.fsockopen.html" ><B CLASS="function" >fsockopen()</B ></A >, <A HREF="function.file.html" ><B CLASS="function" >file()</B ></A >, <A HREF="function.file-exists.html" ><B CLASS="function" >file_exists()</B ></A >, <A HREF="function.is-readable.html" ><B CLASS="function" >is_readable()</B ></A >, <A HREF="function.socket-set-timeout.html" ><B CLASS="function" >socket_set_timeout()</B ></A >, and <A HREF="function.popen.html" ><B CLASS="function" >popen()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.fnmatch.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.fpassthru.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >fnmatch</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.filesystem.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >fpassthru</TD ></TR ></TABLE ></DIV ></BODY ></HTML >