function.fopen.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 441 行 · 第 1/2 页

HTML
441
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Opens file or URL</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div 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> &mdash; <span class="dc-title">Opens file or URL</span></p> </div>  <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type">resource</span> <span class="methodname"><b><b>fopen</b></b></span>    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$filename</tt></span>   , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$mode</tt></span>   [, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$use_include_path</tt></span>   [, <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$context</tt></span>  ]] )</div>  <p class="para rdfs-comment">   <b>fopen()</b> binds a named resource, specified by   <i><tt class="parameter">filename</tt></i>, to a stream.   </p> </div> <div class="refsect1 parameters">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">filename</tt></i></span>     <dd>      <p class="para">       If <i><tt class="parameter">filename</tt></i> is of the form &quot;scheme://...&quot;, 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       <i><tt class="parameter">filename</tt></i> specifies a regular file.      </p>      <p class="para">       If PHP has decided that <i><tt class="parameter">filename</tt></i> 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" class="link">safe mode</a>,       or <a href="features.safe-mode.html#ini.open-basedir" class="link">open_basedir</a> further       restrictions may apply.      </p>      <p class="para">       If PHP has decided that <i><tt class="parameter">filename</tt></i> 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><p><b class="note">Note</b>:                The list of supported protocols can be found in <a href="wrappers.html" class="xref">List of Supported Protocols/Wrappers</a>. Some protocols (also referred to as        <i>wrappers</i>) support <i>context</i>        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 <i>user_agent</i> used by the        <i>http</i> wrapper).       <br />      </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">&lt;?php<br />$handle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"c:\\data\\info.txt"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>        </div>       </div>      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">mode</tt></i></span>     <dd>      <p class="para">       The <i><tt class="parameter">mode</tt></i> parameter specifies the type of access       you require to the stream.  It may be any of the following:       <table border="5">        <caption><b>         A list of possible modes for <b>fopen()</b>         using <i><tt class="parameter">mode</tt></i>        </b></caption>        <colgroup>         <thead valign="middle">          <tr valign="middle">           <th colspan="1"><i><tt class="parameter">mode</tt></i></th>           <th colspan="1">Description</th>          </tr>         </thead>         <tbody valign="middle" class="tbody">          <tr valign="middle">           <td colspan="1" rowspan="1" align="left"><i>&#039;r&#039;</i></td>           <td colspan="1" rowspan="1" align="left">            Open for reading only; place the file pointer at the            beginning of the file.           </td>          </tr>          <tr valign="middle">           <td colspan="1" rowspan="1" align="left"><i>&#039;r+&#039;</i></td>           <td colspan="1" rowspan="1" align="left">            Open for reading and writing; place the file pointer at            the beginning of the file.           </td>          </tr>          <tr valign="middle">           <td colspan="1" rowspan="1" align="left"><i>&#039;w&#039;</i></td>           <td colspan="1" rowspan="1" align="left">            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 valign="middle">           <td colspan="1" rowspan="1" align="left"><i>&#039;w+&#039;</i></td>           <td colspan="1" rowspan="1" align="left">            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 valign="middle">           <td colspan="1" rowspan="1" align="left"><i>&#039;a&#039;</i></td>           <td colspan="1" rowspan="1" align="left">            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 valign="middle">           <td colspan="1" rowspan="1" align="left"><i>&#039;a+&#039;</i></td>           <td colspan="1" rowspan="1" align="left">            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 valign="middle">           <td colspan="1" rowspan="1" align="left"><i>&#039;x&#039;</i></td>           <td colspan="1" rowspan="1" align="left">            Create and open for writing only; place the file pointer at the            beginning of the file.  If the file already exists, the            <b>fopen()</b> call will fail by returning <b><tt>FALSE</tt></b> and            generating an error of level <b><tt>E_WARNING</tt></b>.  If            the file does not exist, attempt to create it.  This is equivalent            to specifying <i>O_EXCL|O_CREAT</i> flags for the            underlying <i>open(2)</i> system call.            </td>          </tr>          <tr valign="middle">           <td colspan="1" rowspan="1" align="left"><i>&#039;x+&#039;</i></td>           <td colspan="1" rowspan="1" align="left">            Create and open for reading and writing; place the file pointer at            the beginning of the file.  If the file already exists, the            <b>fopen()</b> call will fail by returning <b><tt>FALSE</tt></b> and            generating an error of level <b><tt>E_WARNING</tt></b>.  If            the file does not exist, attempt to create it.  This is equivalent            to specifying <i>O_EXCL|O_CREAT</i> flags for the            underlying <i>open(2)</i> system call.           </td>          </tr>         </tbody>        </colgroup>       </table>      </p>      <blockquote><p><b class="note">Note</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

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?