function.file-put-contents.html

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

HTML
296
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Write a string to a file</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.file-get-contents.html">file_get_contents</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.file.html">file</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.file-put-contents" class="refentry"> <div class="refnamediv">  <h1 class="refname">file_put_contents</h1>   <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">file_put_contents</span> &mdash; <span class="dc-title">Write a string to a file</span></p> </div> <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type">int</span> <span class="methodname"><b><b>file_put_contents</b></b></span>    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$filename</tt></span>   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$data</tt></span>   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$flags</tt></span>   [, <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$context</tt></span>  ]] )</div>  <p class="para rdfs-comment">    This function is identical to calling <a href="function.fopen.html" class="function">fopen()</a>,    <a href="function.fwrite.html" class="function">fwrite()</a> and <a href="function.fclose.html" class="function">fclose()</a> successively   to write data to a file.  </p>  <p class="para">   If <i><tt class="parameter">filename</tt></i> does not exist, the file is created.   Otherwise, the existing file is overwritten, unless the    <b><tt>FILE_APPEND</tt></b> flags is set.  </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">       Path to the file where to write the data.      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">data</tt></i></span>     <dd>      <p class="para">       The data to write. Can be either a <a href="language.types.string.html" class="type string">string</a>, an       <a href="language.types.array.html" class="type array">array</a> or a <span class="type stream">stream</span> resource (explained above).      </p>      <p class="para">       If <i><tt class="parameter">data</tt></i> is a <span class="type stream">stream</span> resource, the       remaining buffer of that stream will be copied to the specified file.       This is similar with using <a href="function.stream-copy-to-stream.html" class="function">stream_copy_to_stream()</a>.      </p>      <p class="para">       You can also specify the <i><tt class="parameter">data</tt></i> parameter as a single       dimension array. This is equivalent to       <i>file_put_contents($filename, implode(&#039;&#039;, $array))</i>.      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">flags</tt></i></span>     <dd>      <p class="para">       The value of <i><tt class="parameter">flags</tt></i> can be any combination of        the following flags (with some restrictions), joined with the binary OR        (<i>|</i>) operator.      </p>      <p class="para">       <table border="5">        <caption><b>Available flags</b></caption>        <colgroup>         <thead valign="middle">          <tr valign="middle">           <th colspan="1">Flag</th>           <th colspan="1">Description</th>          </tr>         </thead>         <tbody valign="middle" class="tbody">          <tr valign="middle">           <td colspan="1" rowspan="1" align="left">            <b><tt>FILE_USE_INCLUDE_PATH</tt></b>           </td>           <td colspan="1" rowspan="1" align="left">            Search for <i><tt class="parameter">filename</tt></i> in the include directory.            See <a href="ini.core.html#ini.include-path" class="link">include_path</a> for more            information.           </td>          </tr>          <tr valign="middle">           <td colspan="1" rowspan="1" align="left">            <b><tt>FILE_APPEND</tt></b>           </td>           <td colspan="1" rowspan="1" align="left">            If file <i><tt class="parameter">filename</tt></i> already exists, append             the data to the file instead of overwriting it.           </td>          </tr>          <tr valign="middle">           <td colspan="1" rowspan="1" align="left">            <b><tt>LOCK_EX</tt></b>           </td>           <td colspan="1" rowspan="1" align="left">            Acquire an exclusive lock on the file while proceeding to the             writing.           </td>          </tr>          <tr valign="middle">           <td colspan="1" rowspan="1" align="left">            <b><tt>FILE_TEXT</tt></b>           </td>           <td colspan="1" rowspan="1" align="left">            <i><tt class="parameter">data</tt></i> is written in text mode. If unicode             semantics are enabled, the default encoding is UTF-8.            You can specify a different encoding by creating a custom context            or by using the <b>stream_default_encoding()</b> to            change the default. This flag cannot be used with             <b><tt>FILE_BINARY</tt></b>. This flag is only available since            PHP 6.           </td>          </tr>          <tr valign="middle">           <td colspan="1" rowspan="1" align="left">            <b><tt>FILE_BINARY</tt></b>           </td>           <td colspan="1" rowspan="1" align="left">            <i><tt class="parameter">data</tt></i> will be written in binary mode. This            is the default setting and cannot be used with            <b><tt>FILE_TEXT</tt></b>. This flag is only available since            PHP 6.           </td>          </tr>         </tbody>        </colgroup>       </table>      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">context</tt></i></span>     <dd>      <p class="para">       A valid context resource created with        <a href="function.stream-context-create.html" class="function">stream_context_create()</a>.      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   The function returns the number of bytes that were written to the file, or   <b><tt>FALSE</tt></b> on failure.  </p> </div> <div class="refsect1 changelog">  <h3 class="title">ChangeLog</h3>  <p class="para">   <table class="informaltable">    <colgroup>     <thead valign="middle">      <tr valign="middle">       <th colspan="1">Version</th>       <th colspan="1">Description</th>      </tr>     </thead>     <tbody valign="middle" class="tbody">      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">5.0.0</td>       <td colspan="1" rowspan="1" align="left">        Added context support       </td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">5.1.0</td>       <td colspan="1" rowspan="1" align="left">        Added support for <b><tt>LOCK_EX</tt></b> and the ability to pass        a stream resource to the <i><tt class="parameter">data</tt></i> parameter       </td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">6.0.0</td>       <td colspan="1" rowspan="1" align="left">        Added support for the <b><tt>FILE_TEXT</tt></b> and        <b><tt>FILE_BINARY</tt></b> flags       </td>      </tr>     </tbody>    </colgroup>   </table>  </p> </div>   <div class="refsect1 notes">  <h3 class="title">Notes</h3>  <blockquote><p><b class="note">Note</b>: <span class="simpara">This function isbinary-safe.</span></p></blockquote>  <div class="tip"><b class="tip">Tip</b><p class="simpara">A URL can be used as afilename with this function if the <a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">fopen wrappers</a> have been enabled.See <a href="function.fopen.html" class="function">fopen()</a> for more details on how to specifythe filename and <a href="wrappers.html" class="xref">List of Supported Protocols/Wrappers</a> for a list of supportedURL protocols.</p></div> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a></li>    <li class="member"><a href="function.fwrite.html" class="function" rel="rdfs-seeAlso">fwrite()</a></li>    <li class="member"><a href="function.file-get-contents.html" class="function" rel="rdfs-seeAlso">file_get_contents()</a></li>    <li class="member"><a href="function.stream-context-create.html" class="function" rel="rdfs-seeAlso">stream_context_create()</a></li>   </ul>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.file-get-contents.html">file_get_contents</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.file.html">file</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></body></html>

⌨️ 快捷键说明

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