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

contributors($setup);

?>
<div id="function.file" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">file</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">file</span> &mdash; <span class="dc-title">把整个文件读入一个数组中</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.file-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>file</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$filename</code></span>, <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>, <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>): <span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   把整个文件读入一个数组中。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    你可以通过 <span class="function"><a href="function.file-get-contents.php" class="function">file_get_contents()</a></span> 以字符串形式获取文件的内容。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.file-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">filename</code></dt>
     <dd>
      <p class="para">
       文件的路径。
      </p>
      <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>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       可选参数 <code class="parameter">flags</code> 可以是以下一个或多个常量：
       <dl>
        
         <dt>
          <strong><code><a href="filesystem.constants.php#constant.file-use-include-path">FILE_USE_INCLUDE_PATH</a></code></strong>
         </dt>
         <dd>
          <span class="simpara">
           在 <a href="ini.core.php#ini.include-path" class="link">include_path</a> 中查找文件。
          </span>
         </dd>
        
        
         <dt>
          <strong><code><a href="filesystem.constants.php#constant.file-ignore-new-lines">FILE_IGNORE_NEW_LINES</a></code></strong>
         </dt>
         <dd>
          <span class="simpara">
           省略数组中每个元素末尾的换行符。
          </span>
         </dd>
        
        
         <dt>
          <strong><code><a href="filesystem.constants.php#constant.file-skip-empty-lines">FILE_SKIP_EMPTY_LINES</a></code></strong>
         </dt>
         <dd>
          <span class="simpara">
           跳过空行。
          </span>
         </dd>
        
        
         <dt>
          <strong><code><a href="filesystem.constants.php#constant.file-no-default-context">FILE_NO_DEFAULT_CONTEXT</a></code></strong>
         </dt>
         <dd>
          <span class="simpara">
           不使用默认的上下文。
          </span>
         </dd>
        
       </dl>
      </p>
     </dd>
    
    
     <dt><code class="parameter">context</code></dt>
     <dd>
       <p class="para"><a href="stream.contexts.php" class="link">上下文流（context stream）</a>
<span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>。</p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.file-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回数组形式的文件内容。数组的每个元素对应于文件中的一行（结尾会附加换行符）。
   失败时，<span class="function"><strong>file()</strong></span> 返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    除非使用了 <strong><code><a href="filesystem.constants.php#constant.file-ignore-new-lines">FILE_IGNORE_NEW_LINES</a></code></strong> ，否则结果数组中的每一行都将包含行尾。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">在读取在 Macintosh 电脑中或由其创建的文件时， 如果 PHP
不能正确的识别行结束符，启用运行时配置可选项 <a href="filesystem.configuration.php#ini.auto-detect-line-endings" class="link">auto_detect_line_endings</a>
也许可以解决此问题。</span></p></blockquote>
 </div>


 <div class="refsect1 errors" id="refsect1-function.file-errors">
  <h3 class="title">错误／异常</h3>
  <p class="simpara">
   自 PHP 8.3.0 起，如果 <code class="parameter">flags</code> 包含任意无效值（例如
   <strong><code><a href="filesystem.constants.php#constant.file-append">FILE_APPEND</a></code></strong>），则会抛出 <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span>。
  </p>
  <p class="para">
   如果文件不存在，则发出 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> 级错误。
  </p>
 </div>


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

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.3.0</td>
      <td>
       <code class="parameter">flags</code> 包含任意无效值会抛出 <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span>。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.file-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>file()</strong></span> 示例</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">// 将一个文件读入数组。本例中通过 HTTP 从 URL 中取得 HTML 源文件。<br /><br /></span><span style="color: #0000BB">$lines </span><span style="color: #007700">= </span><span style="color: #0000BB">file</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com/'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// 在数组中循环，显示 HTML 的源文件并加上行号。<br /><br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$lines </span><span style="color: #007700">as </span><span style="color: #0000BB">$line_num </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$line</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"Line #&lt;b&gt;</span><span style="color: #007700">{</span><span style="color: #0000BB">$line_num</span><span style="color: #007700">}</span><span style="color: #DD0000">&lt;/b&gt; : " </span><span style="color: #007700">. </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$line</span><span style="color: #007700">) . </span><span style="color: #DD0000">"&lt;br /&gt;\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// 使用可选的 flags 参数<br /></span><span style="color: #0000BB">$trimmed </span><span style="color: #007700">= </span><span style="color: #0000BB">file</span><span style="color: #007700">(</span><span style="color: #DD0000">'somefile.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">FILE_IGNORE_NEW_LINES </span><span style="color: #007700">| </span><span style="color: #0000BB">FILE_SKIP_EMPTY_LINES</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-notes">
  <h3 class="title">注释</h3>
  <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-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <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.readfile.php" class="function" rel="rdfs-seeAlso">readfile()</a> - 输出文件</span></li>
    <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.fsockopen.php" class="function" rel="rdfs-seeAlso">fsockopen()</a> - 打开 Internet 或者 Unix 套接字连接</span></li>
    <li><span class="function"><a href="function.popen.php" class="function" rel="rdfs-seeAlso">popen()</a> - 打开进程文件指针</span></li>
    <li><span class="function"><a href="function.include.php" class="function" rel="rdfs-seeAlso">include</a> - include</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); ?>