<?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-get-contents.php',
    1 => 'file_get_contents',
    2 => '将整个文件读入一个字符串',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => '文件系统函数',
  ),
  'prev' => 
  array (
    0 => 'function.file-exists.php',
    1 => 'file_exists',
  ),
  'next' => 
  array (
    0 => 'function.file-put-contents.php',
    1 => 'file_put_contents',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/filesystem/functions/file-get-contents.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.file-get-contents" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">file_get_contents</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">file_get_contents</span> &mdash; <span class="dc-title">将整个文件读入一个字符串</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.file-get-contents-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>file_get_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.boolean.php" class="type bool">bool</a></span> <code class="parameter">$use_include_path</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></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>&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">$offset</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.integer.php" class="type int">int</a></span></span> <code class="parameter">$length</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.string.php" class="type string">string</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.file.php" class="function">file()</a></span> 一样，只除了
   <span class="function"><strong>file_get_contents()</strong></span> 把文件读入一个字符串。将在参数
   <code class="parameter">offset</code> 所指定的位置开始读取长度为
   <code class="parameter">length</code> 的内容。如果失败，<span class="function"><strong>file_get_contents()</strong></span>
   将返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
  </p>
  <p class="para">
   <span class="function"><strong>file_get_contents()</strong></span>
   函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    如果要打开有特殊字符的 URL （比如说有空格），就需要使用
    <span class="function"><a href="function.urlencode.php" class="function">urlencode()</a></span> 进行 URL 编码。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.file-get-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">use_include_path</code></dt>
     <dd>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <p class="para">
        常量 <strong><code><a href="filesystem.constants.php#constant.file-use-include-path">FILE_USE_INCLUDE_PATH</a></code></strong> 用于触发搜索
        <a href="ini.core.php#ini.include-path" class="link">include path</a>。
        因为 <strong><code><a href="filesystem.constants.php#constant.file-use-include-path">FILE_USE_INCLUDE_PATH</a></code></strong> 是个 <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>，如果开启了<a href="language.types.declarations.php#language.types.declarations.strict" class="link">严格类型</a> 将无法启用。
        所以要用 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> 来代替常量。
       </p>
      </p></blockquote>
     </dd>
    
    
     <dt><code class="parameter">context</code></dt>
     <dd>
      <p class="para">
       <span class="function"><a href="function.stream-context-create.php" class="function">stream_context_create()</a></span> 创建的有效的上下文（context）资源。
       如果你不需要自定义 context，可以用 <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> 来忽略。
      </p>
     </dd>
    
    
     <dt><code class="parameter">offset</code></dt>
     <dd>
      <p class="para">
       读取原始数据流的开始位置偏移量。负的 offset 会从数据流的末尾开始统计。
      </p>
      <p class="para">
       远程文件不支持偏移量寻址（<code class="parameter">offset</code>）。
       对远程文件以较小的 offset 可能可以正常寻址，
       但由于是对缓冲流进行操作，所以操作结果不可预测。
      </p>
     </dd>
    
    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       要读取数据的最大长度。
       默认情况下会读到文件末尾。
       注意，该参数会应用到处理 stream 的过滤器（filter）中。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.file-get-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 errors" id="refsect1-function.file-get-contents-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   以下情况会导致 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> 级别错误：
   无法找到 <code class="parameter">filename</code> 文件；
   <code class="parameter">length</code> 小于零；
   在 steam 中无法寻址偏移量 <code class="parameter">offset</code>。
  </p>
  <p class="para">
   Windows 下用 <span class="function"><strong>file_get_contents()</strong></span> 读取目录会导致 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> 错误。
   PHP 7.4 起，其他操作系统也会出现同样错误。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.file-get-contents-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        现在 <code class="parameter">length</code> 允许为 null。
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        支持负数 <code class="parameter">offset</code>。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.file-get-contents-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 获取并输出网站首页 HTML 源码</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$homepage </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com/'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$homepage</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 在 include_path 里搜索</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// 如果开启了严格类型，例如 declare(strict_types=1);<br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'./people.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// 否则就这样写<br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'./people.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">FILE_USE_INCLUDE_PATH</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-3">
    <p><strong>示例 #3 读取文件一小节</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// 从第 21 个字符开始，读取 14 字符长度<br /></span><span style="color: #0000BB">$section </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'./people.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #0000BB">20</span><span style="color: #007700">, </span><span style="color: #0000BB">14</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$section</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>以上示例的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">string(14) &quot;lle Bjori Ro&quot;</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-4">
    <p><strong>示例 #4 使用 stream 上下文（context）</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// 创建 stream<br /></span><span style="color: #0000BB">$opts </span><span style="color: #007700">= [<br />  </span><span style="color: #DD0000">'http' </span><span style="color: #007700">=&gt; [<br />    </span><span style="color: #DD0000">'method' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"GET"</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'header' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Accept-language: en\r\n" </span><span style="color: #007700">.<br />                </span><span style="color: #DD0000">"Cookie: foo=bar"</span><span style="color: #007700">,<br />  ]<br />];<br /><br /></span><span style="color: #0000BB">$context </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(</span><span style="color: #0000BB">$opts</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// 以下面设置的 HTTP 头来打开文件<br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com/'</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">$context</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-get-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 class="warning"><strong class="warning">警告</strong><p class="para">使用 SSL 时，Microsoft IIS
会违反协议不发送 <code class="literal">close_notify</code> 标记就关闭连接。PHP 会在到达数据尾端时报告“SSL: Fatal Protocol Error”。
要解决此问题，<a href="errorfunc.configuration.php#ini.error-reporting" class="link">error_reporting</a> 应设定为降低级别至不包含警告。PHP
4.3.7 及更高版本可以在使用 <code class="literal">https://</code> 包装器打开流时检测出有问题的 IIS 服务器软件 并抑制警告。在使用
<span class="function"><a href="function.fsockopen.php" class="function">fsockopen()</a></span> 创建 <code class="literal">ssl://</code> 套接字时，开发者需检测并抑制此警告。</p></div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.file-get-contents-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.file.php" class="function" rel="rdfs-seeAlso">file()</a> - 把整个文件读入一个数组中</span></li>
    <li><span class="function"><a href="function.fgets.php" class="function" rel="rdfs-seeAlso">fgets()</a> - 从文件指针中读取一行</span></li>
    <li><span class="function"><a href="function.fread.php" class="function" rel="rdfs-seeAlso">fread()</a> - 读取文件（可安全用于二进制文件）</span></li>
    <li><span class="function"><a href="function.readfile.php" class="function" rel="rdfs-seeAlso">readfile()</a> - 输出文件</span></li>
    <li><span class="function"><a href="function.file-put-contents.php" class="function" rel="rdfs-seeAlso">file_put_contents()</a> - 将数据写入文件</span></li>
    <li><span class="function"><a href="function.stream-get-contents.php" class="function" rel="rdfs-seeAlso">stream_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>
    <li><a href="reserved.variables.httpresponseheader.php" class="link">$http_response_header</a></li>
   </ul>
  </p>
 </div>

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