<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.filesystem.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.file-put-contents.php',
    1 => 'file_put_contents',
    2 => '将数据写入文件',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => '文件系统函数',
  ),
  'prev' => 
  array (
    0 => 'function.file-get-contents.php',
    1 => 'file_get_contents',
  ),
  'next' => 
  array (
    0 => 'function.fileatime.php',
    1 => 'fileatime',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/filesystem/functions/file-put-contents.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.file-put-contents" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">file_put_contents</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">file_put_contents</span> &mdash; <span class="dc-title">将数据写入文件</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.file-put-contents-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>file_put_contents</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$filename</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$data</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span></span> <code class="parameter">$context</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span><br>): <span class="type"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment"> 
   和依次调用 <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>，<span class="function"><a href="function.fwrite.php" class="function">fwrite()</a></span>
   以及 <span class="function"><a href="function.fclose.php" class="function">fclose()</a></span> 功能一样。
  </p>
  <p class="para">
   如果 <code class="parameter">filename</code>
   不存在，将会创建文件。反之，存在的文件将会重写，除非设置
   <strong><code><a href="filesystem.constants.php#constant.file-append">FILE_APPEND</a></code></strong> flag。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.file-put-contents-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">filename</code></dt>
     <dd>
      <p class="para">
       要被写入数据的文件名。
      </p>
     </dd>
    
    
     <dt><code class="parameter">data</code></dt>
     <dd>
      <p class="para">
       要写入的数据。类型可以是 <span class="type"><a href="language.types.string.php" class="type string">string</a></span>，<span class="type"><a href="language.types.array.php" class="type array">array</a></span>
       或者是 <span class="type">stream</span> 资源（如上面所说的那样）。
      </p>
      <p class="para">
       如果 <code class="parameter">data</code>
       指定为 stream 资源，这里 stream
       中所保存的缓存数据将被写入到指定文件中，这种用法就相似于使用
       <span class="function"><a href="function.stream-copy-to-stream.php" class="function">stream_copy_to_stream()</a></span> 函数。
      </p>
      <p class="para">
       参数 <code class="parameter">data</code> 可以是数组（但不能为多维数组），这就相当于
       <code class="literal">file_put_contents($filename, join(&#039;&#039;, $array))</code>。
      </p>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">flags</code> 的值可以是
       以下 flag 使用 OR (<code class="literal">|</code>) 运算符进行的组合。
      </p>
       <p class="para">
       <table class="doctable table">
        <caption><strong>Available flags</strong></caption>
        
         <thead>
          <tr>
           <th>Flag</th>
           <th>描述</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td>
            <strong><code><a href="filesystem.constants.php#constant.file-use-include-path">FILE_USE_INCLUDE_PATH</a></code></strong>
           </td>
           <td>
            在 include 目录里搜索  <code class="parameter">filename</code>。
            更多信息可参见 <a href="ini.core.php#ini.include-path" class="link">include_path</a>。
           </td>
          </tr>

          <tr>
           <td>
            <strong><code><a href="filesystem.constants.php#constant.file-append">FILE_APPEND</a></code></strong>
           </td>
           <td>
            如果文件 <code class="parameter">filename</code>  已经存在，追加数据而不是覆盖。
           </td>
          </tr>

          <tr>
           <td>
            <strong><code><a href="filesystem.constants.php#constant.lock-ex">LOCK_EX</a></code></strong>
           </td>
           <td>
            在写入时获取文件独占锁。换句话说，在调用 <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>
            和 <span class="function"><a href="function.fwrite.php" class="function">fwrite()</a></span> 中间发生了 <span class="function"><a href="function.flock.php" class="function">flock()</a></span>
            调用。这与调用带模式“x”的 <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> 不同。
           </td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>
    
    
     <dt><code class="parameter">context</code></dt>
     <dd>
      <p class="para">
       一个 context 资源。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.file-put-contents-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   该函数将返回写入到文件内数据的字节数，失败时返回<strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>
  </p>
  <div class="warning"><strong class="warning">警告</strong><p class="simpara">此函数可能返回布尔值
<strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>，但也可能返回等同于 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> 的非布尔值。请阅读 <a href="language.types.boolean.php" class="link">布尔类型</a>章节以获取更多信息。应使用
<a href="language.operators.comparison.php" class="link">===
运算符</a>来测试此函数的返回值。</p></div>
 </div>


 <div class="refsect1 examples" id="refsect1-function.file-put-contents-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 简单用法示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$file </span><span style="color: #007700">= </span><span style="color: #DD0000">'people.txt'</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// 打开文件获取已经存在的内容<br /></span><span style="color: #0000BB">$current </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// 追加新成员到文件<br /></span><span style="color: #0000BB">$current </span><span style="color: #007700">.= </span><span style="color: #DD0000">"John Smith\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// 将内容写回文件<br /></span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">, </span><span style="color: #0000BB">$current</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-2">
    <p><strong>示例 #2 Using flags</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$file </span><span style="color: #007700">= </span><span style="color: #DD0000">'people.txt'</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// The new person to add to the file<br /></span><span style="color: #0000BB">$person </span><span style="color: #007700">= </span><span style="color: #DD0000">"John Smith\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// Write the contents to the file, <br />// using the FILE_APPEND flag to append the content to the end of the file<br />// and the LOCK_EX flag to prevent anyone else writing to the file at the same time<br /></span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">, </span><span style="color: #0000BB">$person</span><span style="color: #007700">, </span><span style="color: #0000BB">FILE_APPEND </span><span style="color: #007700">| </span><span style="color: #0000BB">LOCK_EX</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.file-put-contents-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">此函数可安全用于二进制对象。</span></p></blockquote>
  <div class="tip"><strong class="tip">小技巧</strong><p class="simpara">如已启用<a href="filesystem.configuration.php#ini.allow-url-fopen" class="link">fopen 包装器</a>，在此函数中， URL 可作为文件名。关于如何指定文件名详见 <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>。各种
wapper 的不同功能请参见 <a href="wrappers.php" class="xref">支持的协议和封装协议</a>，注意其用法及其可提供的预定义变量。</p></div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.file-put-contents-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.fopen.php" class="function" rel="rdfs-seeAlso">fopen()</a> - 打开文件或者 URL</span></li>
    <li><span class="function"><a href="function.fwrite.php" class="function" rel="rdfs-seeAlso">fwrite()</a> - 写入文件（可安全用于二进制文件）</span></li>
    <li><span class="function"><a href="function.file-get-contents.php" class="function" rel="rdfs-seeAlso">file_get_contents()</a> - 将整个文件读入一个字符串</span></li>
    <li><span class="function"><a href="function.stream-context-create.php" class="function" rel="rdfs-seeAlso">stream_context_create()</a> - 创建资源流上下文</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>