<?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.fgets.php',
    1 => 'fgets',
    2 => '从文件指针中读取一行',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => '文件系统函数',
  ),
  'prev' => 
  array (
    0 => 'function.fgetcsv.php',
    1 => 'fgetcsv',
  ),
  'next' => 
  array (
    0 => 'function.fgetss.php',
    1 => 'fgetss',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/filesystem/functions/fgets.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.fgets" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fgets</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">fgets</span> &mdash; <span class="dc-title">从文件指针中读取一行</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.fgets-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fgets</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"><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>): <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">
   从文件指针中读取一行。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.fgets-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="function"><a href="function.fsockopen.php" class="function">fsockopen()</a></span> 成功打开的文件(并还未由 <span class="function"><a href="function.fclose.php" class="function">fclose()</a></span> 关闭)。</p>
     </dd>
    
    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       从 <code class="parameter">handle</code>
       指向的文件中读取一行并返回长度最多为 <code class="parameter">length</code> - 1
       字节的字符串。碰到换行符（包括在返回值中）、EOF 或者已经读取了 length - 1
       字节后停止（看先碰到那一种情况）。如果没有指定
       <code class="parameter">length</code>，则默认为 1K，或者说 1024 字节。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.fgets-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   从指针 <code class="parameter">stream</code> 指向的文件中读取了 <code class="parameter">length</code> - 1 字节后返回字符串。 
   如果文件指针中没有更多的数据了则返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
  </p>
  <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.fgets-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 /><br />$fp </span><span style="color: #007700">= @</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"/tmp/inputfile.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /><br />if (</span><span style="color: #0000BB">$fp</span><span style="color: #007700">) {<br />    while ((</span><span style="color: #0000BB">$buffer </span><span style="color: #007700">= </span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">4096</span><span style="color: #007700">)) !== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br />        echo </span><span style="color: #0000BB">$buffer</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />    }<br /><br />    if (!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)) {<br />        echo </span><span style="color: #DD0000">"Error: unexpected fgets() fail\n"</span><span style="color: #007700">;<br />    }<br /><br />    </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

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


 <div class="refsect1 notes" id="refsect1-function.fgets-notes">
  <h3 class="title">注释</h3>
  <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>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    习惯了 C 语言中 <span class="function"><strong>fgets()</strong></span> 语法的人应该注意到 <code class="literal">EOF</code>
   是怎样被返回的。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.fgets-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <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.fread.php" class="function" rel="rdfs-seeAlso">fread()</a> - 读取文件（可安全用于二进制文件）</span></li>
    <li><span class="function"><a href="function.fgetc.php" class="function" rel="rdfs-seeAlso">fgetc()</a> - 从文件指针中读取字符</span></li>
    <li><span class="function"><a href="function.stream-get-line.php" class="function" rel="rdfs-seeAlso">stream_get_line()</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.popen.php" class="function" rel="rdfs-seeAlso">popen()</a> - 打开进程文件指针</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.stream-set-timeout.php" class="function" rel="rdfs-seeAlso">stream_set_timeout()</a> - Set timeout period on a stream</span></li>
   </ul>
  </p>
 </div>

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