<?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.fread.php',
    1 => 'fread',
    2 => '读取文件（可安全用于二进制文件）',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => '文件系统函数',
  ),
  'prev' => 
  array (
    0 => 'function.fputs.php',
    1 => 'fputs',
  ),
  'next' => 
  array (
    0 => 'function.fscanf.php',
    1 => 'fscanf',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/filesystem/functions/fread.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.fread" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fread</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">fread</span> &mdash; <span class="dc-title">读取文件（可安全用于二进制文件）</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.fread-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fread</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> <code class="parameter">$stream</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$length</code></span>): <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"><strong>fread()</strong></span> 从文件指针
   <code class="parameter">stream</code> 读取最多
   <code class="parameter">length</code> 个字节。
   
   该函数在遇上以下几种情况时停止读取文件：
   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
      读取了 <code class="parameter">length</code> 个字节
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      到达了文件末尾（EOF）
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      a packet becomes available or the <a href="function.socket-set-timeout.php" class="link">
      socket timeout</a> occurs (for network streams)
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      if the stream is read buffered and it does not represent a plain file, at
      most one read of up to a number of bytes equal to the chunk size (usually
      8192) is made; depending on the previously buffered data, the size of the
      returned data may be larger than the chunk size.
     </span>
    </li>
   </ul>
  </p>
 </div>

 
  <div class="refsect1 parameters" id="refsect1-function.fread-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">stream</code></dt>
     <dd>
      <p class="para">文件系统指针，是典型地由
<span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> 创建的 <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>(资源)。</p>
     </dd>
    
    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       最多读取 <code class="parameter">length</code> 个字节。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 
  <div class="refsect1 returnvalues" id="refsect1-function.fread-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回所读取的字符串， 或者在失败时返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。 
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.fread-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 一个简单的 <span class="function"><strong>fread()</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">// get contents of a file into a string<br /></span><span style="color: #0000BB">$filename </span><span style="color: #007700">= </span><span style="color: #DD0000">"/usr/local/something.txt"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$contents </span><span style="color: #007700">= </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">, </span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>示例 #2 Binary <span class="function"><strong>fread()</strong></span> example</strong></p>
    <div class="warning"><strong class="warning">警告</strong>
     <p class="para">
     在区分二进制文件和文本文件的系统上（如
    Windows）打开文件时，<span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> 函数的 mode 参数要加上 &#039;b&#039;。
     </p>
    </div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$filename </span><span style="color: #007700">= </span><span style="color: #DD0000">"c:\\files\\somepic.gif"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #DD0000">"rb"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$contents </span><span style="color: #007700">= </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">, </span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>示例 #3 Remote <span class="function"><strong>fread()</strong></span> examples</strong></p>
    <div class="warning"><strong class="warning">警告</strong>
     <p class="para">
      当从任何不是普通本地文件读取时，例如在读取从<a href="features.remote-files.php" class="link">远程文件</a>或
    <span class="function"><a href="function.popen.php" class="function">popen()</a></span> 以及 <span class="function"><a href="function.fsockopen.php" class="function">fsockopen()</a></span>
    返回的流时，读取会在一个包可用之后停止。这意味着应该如下例所示将数据收集起来合并成大块。
     </p>
    </div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.com/"</span><span style="color: #007700">, </span><span style="color: #DD0000">"rb"</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">FALSE </span><span style="color: #007700">=== </span><span style="color: #0000BB">$handle</span><span style="color: #007700">) {<br />    exit(</span><span style="color: #DD0000">"Failed to open stream to URL"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$contents </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.com/"</span><span style="color: #007700">, </span><span style="color: #DD0000">"rb"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$contents </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">;<br />while (!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">)) {<br />    </span><span style="color: #0000BB">$contents </span><span style="color: #007700">.= </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">, </span><span style="color: #0000BB">8192</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</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.fread-notes">
  <h3 class="title">注释</h3>
  <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>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    Note that <span class="function"><strong>fread()</strong></span> reads from the current position of 
    the file pointer. Use <span class="function"><a href="function.ftell.php" class="function">ftell()</a></span> to find the current 
    position of the pointer and <span class="function"><a href="function.rewind.php" class="function">rewind()</a></span> to rewind the 
    pointer position.
   </p>
  </p></blockquote>
 </div>

 
  <div class="refsect1 seealso" id="refsect1-function.fread-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <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.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.fgets.php" class="function" rel="rdfs-seeAlso">fgets()</a> - 从文件指针中读取一行</span></li>
    <li><span class="function"><a href="function.fgetss.php" class="function" rel="rdfs-seeAlso">fgetss()</a> - 从文件指针中读取一行并过滤掉 HTML 标记</span></li>
    <li><span class="function"><a href="function.fscanf.php" class="function" rel="rdfs-seeAlso">fscanf()</a> - 从文件中格式化输入</span></li>
    <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.fpassthru.php" class="function" rel="rdfs-seeAlso">fpassthru()</a> - 输出文件指针处的所有剩余数据</span></li>
    <li><span class="function"><a href="function.fseek.php" class="function" rel="rdfs-seeAlso">fseek()</a> - 在文件指针中定位</span></li>
    <li><span class="function"><a href="function.ftell.php" class="function" rel="rdfs-seeAlso">ftell()</a> - 返回文件指针读/写的位置</span></li>
    <li><span class="function"><a href="function.rewind.php" class="function" rel="rdfs-seeAlso">rewind()</a> - 倒回文件指针的位置</span></li>
    <li><span class="function"><a href="function.unpack.php" class="function" rel="rdfs-seeAlso">unpack()</a> - Unpack data from binary string</span></li>
   </ul>
  </p>
 </div>

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