<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >fopen</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" 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-2"></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" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.fnmatch.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.fpassthru.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.fopen" ></A >fopen</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN28358" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >fopen -- Opens file or URL</DIV ><DIV CLASS="refsect1" ><A NAME="AEN28361" ></A ><H2 >Description</H2 >resource <B CLASS="methodname" >fopen</B > ( string filename, string mode [, bool use_include_path [, resource zcontext]])<BR ></BR ><P > <B CLASS="function" >fopen()</B > binds a named resource, specified by <VAR CLASS="parameter" >filename</VAR >, to a stream. If <VAR CLASS="parameter" >filename</VAR > 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 <VAR CLASS="parameter" >filename</VAR > specifies a regular file. </P ><P > If PHP has decided that <VAR CLASS="parameter" >filename</VAR > 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 <VAR CLASS="parameter" >filename</VAR > 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 >Notã: </B > The list of supported protocols can be found in <A HREF="wrappers.html" >Anexa L</A >. Some protocols (also referred to as <VAR CLASS="literal" >wrappers</VAR >) support <VAR CLASS="literal" >context</VAR > and/or <TT CLASS="filename" >php.ini</TT > options. Refer to the specific page for the protocol in use for a list of options which can be set. ( i.e. <TT CLASS="filename" >php.ini</TT > value <VAR CLASS="literal" >user_agent</VAR > used by the <VAR CLASS="literal" >http</VAR > wrapper) For a description of <VAR CLASS="literal" >contexts</VAR > and the <VAR CLASS="parameter" >zcontext</VAR > parameter , refer to <A HREF="ref.stream.html" >Referinþã CVIII, <I >Stream Functions</I ></A >. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B >Context support was added with PHP 5.0.0.</P ></BLOCKQUOTE ></DIV ><P > The <VAR CLASS="parameter" >mode</VAR > parameter specifies the type of access you require to the stream. It may be any of the following: <DIV CLASS="table" ><A NAME="AEN28406" ></A ><P ><B >Tabel 1. A list of possible modes for <B CLASS="function" >fopen()</B > using <VAR CLASS="parameter" >mode</VAR > </B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH ><VAR CLASS="parameter" >mode</VAR ></TH ><TH >Description</TH ></TR ></THEAD ><TBODY ><TR ><TD ><VAR CLASS="literal" >'r'</VAR ></TD ><TD > Open for reading only; place the file pointer at the beginning of the file. </TD ></TR ><TR ><TD ><VAR CLASS="literal" >'r+'</VAR ></TD ><TD > Open for reading and writing; place the file pointer at the beginning of the file. </TD ></TR ><TR ><TD ><VAR CLASS="literal" >'w'</VAR ></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 ><VAR CLASS="literal" >'w+'</VAR ></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 ><VAR CLASS="literal" >'a'</VAR ></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 ><VAR CLASS="literal" >'a+'</VAR ></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 ><VAR CLASS="literal" >'x'</VAR ></TD ><TD > Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the <B CLASS="function" >fopen()</B > call will fail by returning <TT CLASS="constant" ><B >FALSE</B ></TT > and generating an error of level <TT CLASS="constant" ><B >E_WARNING</B ></TT >. If the file does not exist, attempt to create it. This is equivalent to specifying <VAR CLASS="literal" >O_EXCL|O_CREAT</VAR > flags for the underlying <VAR CLASS="literal" >open(2)</VAR > system call. This option is supported in PHP 4.3.2 and later, and only works for local files. </TD ></TR ><TR ><TD ><VAR CLASS="literal" >'x+'</VAR ></TD ><TD > Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the <B CLASS="function" >fopen()</B > call will fail by returning <TT CLASS="constant" ><B >FALSE</B ></TT > and generating an error of level <TT CLASS="constant" ><B >E_WARNING</B ></TT >. If the file does not exist, attempt to create it. This is equivalent to specifying <VAR CLASS="literal" >O_EXCL|O_CREAT</VAR > flags for the underlying <VAR CLASS="literal" >open(2)</VAR > system call. This option is supported in PHP 4.3.2 and later, and only works for local files. </TD ></TR ></TBODY ></TABLE ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > 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 <VAR CLASS="literal" >\n</VAR > as the line ending character, Windows based systems use <VAR CLASS="literal" >\r\n</VAR > as the line ending characters and Macintosh based systems use <VAR CLASS="literal" >\r</VAR > as the line ending character. </P ><P > 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 > Windows offers a text-mode translation flag (<VAR CLASS="literal" >'t'</VAR >) which will transparently translate <VAR CLASS="literal" >\n</VAR > to <VAR CLASS="literal" >\r\n</VAR > when working with the file. In contrast, you can also use <VAR CLASS="literal" >'b'</VAR > to force binary mode, which will not translate your data. To use these flags, specify either <VAR CLASS="literal" >'b'</VAR > or <VAR CLASS="literal" >'t'</VAR > as the last character of the <VAR CLASS="parameter" >mode</VAR > parameter. </P ><P > 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 <VAR CLASS="literal" >'t'</VAR > mode if you are working with plain-text files and you use <VAR CLASS="literal" >\n</VAR > to delimit your line endings in your script, but expect your files to be readable with applications such as notepad. You should use the <VAR CLASS="literal" >'b'</VAR > in all other cases. </P ><P > 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 <VAR CLASS="literal" >\r\n</VAR > characters. </P ><P > <SPAN CLASS="emphasis" ><I CLASS="emphasis" >For portability, it is strongly recommended that you always use the 'b' flag when opening files with <B CLASS="function" >fopen()</B >. </I ></SPAN > </P ><P > <SPAN CLASS="emphasis" ><I CLASS="emphasis" >Again, for portability, it is also strongly recommended that you re-write code that uses or relies upon the <VAR CLASS="literal" >'t'</VAR > mode so that it uses the correct line endings and <VAR CLASS="literal" >'b'</VAR > mode instead. </I ></SPAN > </P ><P > <SPAN CLASS="emphasis" ><I CLASS="emphasis" >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 <VAR CLASS="literal" >'t'</VAR > flag as a workaround until you have made your script more portable as mentioned above. </I ></SPAN > </P ></BLOCKQUOTE ></DIV ><P > The optional third <VAR CLASS="parameter" >use_include_path</VAR > 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="ini.sect.path-directory.html#ini.include-path" >include_path</A >, too. </P ><P > If the open fails, the function returns <TT CLASS="constant" ><B >FALSE</B ></TT > and an error of level <TT CLASS="constant" ><B >E_WARNING</B ></TT > is generated. You may use <A HREF="language.operators.errorcontrol.html" >@</A > to suppress this warning. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN28498" ></A ><P ><B >Exemplu 1. <B CLASS="function" >fopen()</B > examples</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$handle </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">"/home/rasmus/file.txt"</font><font color="#007700">, </font><font color="#DD0000">"r"</font><font color="#007700">);<br /></font><font color="#0000BB">$handle </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">"/home/rasmus/file.gif"</font><font color="#007700">, </font><font color="#DD0000">"wb"</font><font color="#007700">);<br /></font><font color="#0000BB">$handle </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">"http://www.example.com/"</font><font color="#007700">, </font><font color="#DD0000">"r"</font><font color="#007700">);<br /></font><font color="#0000BB">$handle </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">"ftp://user:password@example.com/somefile.txt"</font><font color="#007700">, </font><font color="#DD0000">"w"</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></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 ><A NAME="AEN28504" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$handle </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">"c:\\data\\info.txt"</font><font color="#007700">, </font><font color="#DD0000">"r"</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Avertisment</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >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 workaround this, you should lower your <A HREF="missing-stuff.html#ini.error-reporting" >error_reporting</A > level not to include warnings. PHP 4.3.7 and higher can detect buggy IIS server software when you open the stream using the https:// wrapper and will suppress the warning for you. If you are using <A HREF="function.fsockopen.html" ><B CLASS="function" >fsockopen()</B ></A > to create an ssl:// socket, you are responsible for detecting and suppressing the warning yourself. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B >Când este activat <A HREF="features.safe-mode.html" >safe-mode</A >, PHP verificã dacã directorul în care sînteþi pe cale sã lucraþi are acelaºi UID ca scriptul care se executã.</P ></BLOCKQUOTE ></DIV ><P > See also <A HREF="wrappers.html" >Anexa L</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.fread.html" ><B CLASS="function" >fread()</B ></A >, <A HREF="function.fwrite.html" ><B CLASS="function" >fwrite()</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.stream-set-timeout.html" ><B CLASS="function" >stream_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" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.fpassthru.html" ACCESSKEY="N" >Înainte</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" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >fpassthru</TD ></TR ></TABLE ></DIV ></BODY ></HTML >