<HTML ><HEAD ><TITLE >fopen</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.44"><LINK REL="HOME" TITLE="PHP Manual" HREF="manual.html"><LINK REL="UP" TITLE="Filesystem functions" HREF="ref.filesystem.html"><LINK REL="PREVIOUS" TITLE="flock" HREF="function.flock.html"><LINK REL="NEXT" TITLE="fpassthru" HREF="function.fpassthru.html"></HEAD ><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><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.flock.html" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.fpassthru.html" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.fopen" >fopen</A ></H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8781" ></A >fopen -- Open file or URL</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8784" ></A ><H2 >Description</H2 ><DIV CLASS="funcsynopsis" ><P ></P ><CODE CLASS="FUNCDEF" >int fopen</CODE >(string filename, string mode, int [<SPAN CLASS="optional" >use_include_path</SPAN >] );<P ></P ></DIV ><P > If <TT CLASS="parameter" ><I >filename</I ></TT > begins with "http://" (not case sensitive), an HTTP 1.0 connection is opened to the specified server and a file pointer is returned to the beginning of the text of the response. </P ><P > Does not handle HTTP redirects, so you must include trailing slashes on directories. </P ><P > If <TT CLASS="parameter" ><I >filename</I ></TT > begins with "ftp://" (not case sensitive), an ftp connection to the specified server is opened and a pointer to the requested file is returned. If the server does not support passive mode ftp, this will fail. You can open files for either reading and writing via ftp (but not both simultaneously). </P ><P > If <TT CLASS="parameter" ><I >filename</I ></TT > is one of "php://stdin", "php://stdout", or "php://stderr", the corresponding stdio stream will be opened. (This was introduced in PHP 3.0.13; in earlier versions, a filename such as "/dev/stdin" or "/dev/fd/0" must be used to access the stdio streams.) </P ><P > If <TT CLASS="parameter" ><I >filename</I ></TT > begins with anything else, the file will be opened from the filesystem, and a file pointer to the file opened is returned. </P ><P > If the open fails, the function returns false. </P ><P > <TT CLASS="parameter" ><I >mode</I ></TT > 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 > As well, <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., it's useless on Unix). If not needed, this will be ignored. </P ><P > You can use the optional third parameter and set it to "1", if you want to search for the file in the <A HREF="configuration.html#ini.include-path" >include_path</A >, too. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><P ><B >Example 1. fopen() example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="programlisting" > 1 2 $fp = fopen("/home/rasmus/file.txt", "r"); 3 $fp = fopen("http://www.php.net/", "r"); 4 $fp = fopen("ftp://user:password@example.com/", "w"); 5 </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" ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="programlisting" > 1 2 $fp = fopen("c:\\data\\info.txt", "r"); 3 </PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > See also <A HREF="function.fclose.html" ><B CLASS="function" >fclose()</B ></A >, <A HREF="function.fsockopen.html" ><B CLASS="function" >fsockopen()</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 WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.flock.html" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="manual.html" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.fpassthru.html" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >flock</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.filesystem.html" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >fpassthru</TD ></TR ></TABLE ></DIV ></BODY ></HTML >