fopen Opens file or URL Description resourcefopen stringfilename stringmode intuse_include_path resourcezcontext fopen binds a named resource, specified by filename, to a stream. If 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 filename 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 &safemode;, or open_basedir further restrictions may apply. If PHP has decided that filename specifies a registered protocol, and that protocol is registered as a network URL, PHP will check to make sure that allow_url_fopen is 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 . Some protocols (also referred to as wrappers) support context and/or &php.ini; options. Refer to the specific page for the protocol in use for a list of options which can be set. ( i.e. &php.ini; value user_agent used by the http wrapper) For a description of contexts and the zcontext parameter , refer to . The mode parameter specifies the type of access you require to the stream. It may be any of the following: A list of possible modes for <function>fopen</function> using <parameter>mode</parameter> mode Description 'r' Open for reading only; place the file pointer at the beginning of the file. 'r+' Open for reading and writing; place the file pointer at the beginning of the file. '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. '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. '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. '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.
On systems that differentiate between binary ('b') and text ('t') mode translation (such as Windows), the mode may contain either the letter 'b' or the letter 't' as the last character to force the translation mode to be either binary or text mode respectively. The default translation mode depends on the SAPI that you are using, so you are encouraged to always specify the appropriate flag; you will usually always want to specify 'b' if you intend for your script to be portable between different platforms. If you do not specify the 'b' flag when working with binary files, you will experience strange problems with your data, including broken image files and strange problems with \r\n characters. It is strongly recommended that you always use the 'b' flag when opening files with fopen. The optional third use_include_path parameter can be set to '1' or &true; if you want to search for the file in the include_path, too. If the open fails, the function returns &false;. <function>fopen</function> examples ]]> 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. On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes. ]]> See also , fclose, fgets, fsockopen, file, file_exists, is_readable, socket_set_timeout, and popen.