<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.ftp.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.ftp-nb-get.php',
    1 => 'ftp_nb_get',
    2 => '从 FTP 服务器上获取文件并写入本地文件（non-blocking）',
  ),
  'up' => 
  array (
    0 => 'ref.ftp.php',
    1 => 'FTP 函数',
  ),
  'prev' => 
  array (
    0 => 'function.ftp-nb-fput.php',
    1 => 'ftp_nb_fput',
  ),
  'next' => 
  array (
    0 => 'function.ftp-nb-put.php',
    1 => 'ftp_nb_put',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/ftp/functions/ftp-nb-get.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.ftp-nb-get" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ftp_nb_get</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">ftp_nb_get</span> &mdash; <span class="dc-title">从 FTP 服务器上获取文件并写入本地文件（non-blocking）</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.ftp-nb-get-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>ftp_nb_get</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="class.ftp-connection.php" class="type FTP\Connection">FTP\Connection</a></span> <code class="parameter">$ftp</code></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">$local_filename</code></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">$remote_filename</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">$mode</code><span class="initializer"> = <strong><code><a href="ftp.constants.php#constant.ftp-binary">FTP_BINARY</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>): <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"><strong>ftp_nb_get()</strong></span> 在 FTP 服务器上获取指定的远程文件，并保存到本地。
  </p>
  <p class="para">
    和 <span class="function"><a href="function.ftp-get.php" class="function">ftp_get()</a></span> 不同之处，在于此函数是通过异步的方式来获取文件，这意味着，你的程序可以在下载文件的同时，同步进行其它操作。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.ftp-nb-get-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">ftp</code></dt>
     <dd>
       <p class="para"><span class="classname"><a href="class.ftp-connection.php" class="classname">FTP\Connection</a></span> 实例。</p>
     </dd>
    
    
     <dt><code class="parameter">local_filename</code></dt>
     <dd>
      <p class="para">
        保存到的本地文件路径（如果文件已存在会被覆盖）。
      </p>
     </dd>
    
    
     <dt><code class="parameter">remote_filename</code></dt>
     <dd>
      <p class="para">
        远程文件路径。
      </p>
     </dd>
    
    
     <dt><code class="parameter">mode</code></dt>
     <dd>
      <p class="para">
        指定传输模式。必须是 <strong><code><a href="ftp.constants.php#constant.ftp-ascii">FTP_ASCII</a></code></strong> 或 <strong><code><a href="ftp.constants.php#constant.ftp-binary">FTP_BINARY</a></code></strong>。
      </p>
     </dd>
    
    
     <dt><code class="parameter">offset</code></dt>
     <dd>
      <p class="para">开始下载文件的起始位置。</p>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.ftp-nb-get-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回 <strong><code><a href="ftp.constants.php#constant.ftp-failed">FTP_FAILED</a></code></strong> 或 <strong><code><a href="ftp.constants.php#constant.ftp-finished">FTP_FINISHED</a></code></strong>
   或 <strong><code><a href="ftp.constants.php#constant.ftp-moredata">FTP_MOREDATA</a></code></strong>，或者在无法打开本地文件的情况下返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> 
  </p>
 </div>


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

    </thead>

    <tbody class="tbody">
     <tr>
 <td>8.1.0</td>
 <td>
  现在 <code class="parameter">ftp</code> 参数接受 <span class="classname"><a href="class.ftp-connection.php" class="classname">FTP\Connection</a></span>
  实例，之前接受 <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>。
 </td>
</tr>

     <tr>
      <td>7.3.0</td>
      <td>
       <code class="parameter">mode</code> 参数变为可选，之前是强制性的。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.ftp-nb-get-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>ftp_nb_get()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">// 初始化<br /></span><span style="color: #0000BB">$ret </span><span style="color: #007700">= </span><span style="color: #0000BB">ftp_nb_get</span><span style="color: #007700">(</span><span style="color: #0000BB">$ftp</span><span style="color: #007700">, </span><span style="color: #DD0000">"test"</span><span style="color: #007700">, </span><span style="color: #DD0000">"README"</span><span style="color: #007700">, </span><span style="color: #0000BB">FTP_BINARY</span><span style="color: #007700">);<br />while (</span><span style="color: #0000BB">$ret </span><span style="color: #007700">== </span><span style="color: #0000BB">FTP_MOREDATA</span><span style="color: #007700">) {<br />   <br />   </span><span style="color: #FF8000">// 可以同步干其它事<br />   </span><span style="color: #007700">echo </span><span style="color: #DD0000">"."</span><span style="color: #007700">;<br /><br />   </span><span style="color: #FF8000">// 继续下载...<br />   </span><span style="color: #0000BB">$ret </span><span style="color: #007700">= </span><span style="color: #0000BB">ftp_nb_continue</span><span style="color: #007700">(</span><span style="color: #0000BB">$ftp</span><span style="color: #007700">);<br />}<br />if (</span><span style="color: #0000BB">$ret </span><span style="color: #007700">!= </span><span style="color: #0000BB">FTP_FINISHED</span><span style="color: #007700">) {<br />   echo </span><span style="color: #DD0000">"下载文件出错..."</span><span style="color: #007700">;<br />   exit(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-2">
    <p><strong>示例 #2 通过 <span class="function"><strong>ftp_nb_get()</strong></span> 恢复下载一个文件</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">// 初始化 <br /></span><span style="color: #0000BB">$ret </span><span style="color: #007700">= </span><span style="color: #0000BB">ftp_nb_get</span><span style="color: #007700">(</span><span style="color: #0000BB">$ftp</span><span style="color: #007700">, </span><span style="color: #DD0000">"test"</span><span style="color: #007700">, </span><span style="color: #DD0000">"README"</span><span style="color: #007700">, </span><span style="color: #0000BB">FTP_BINARY</span><span style="color: #007700">, <br />                      </span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #DD0000">"test"</span><span style="color: #007700">));<br /></span><span style="color: #FF8000">// OR: $ret = ftp_nb_get($ftp, "test", "README", <br />//                           FTP_BINARY, FTP_AUTORESUME);<br /></span><span style="color: #007700">while (</span><span style="color: #0000BB">$ret </span><span style="color: #007700">== </span><span style="color: #0000BB">FTP_MOREDATA</span><span style="color: #007700">) {<br />   <br />   </span><span style="color: #FF8000">// 做你爱做的事<br />   </span><span style="color: #007700">echo </span><span style="color: #DD0000">"."</span><span style="color: #007700">;<br /><br />   </span><span style="color: #FF8000">// 继续下载Ing...<br />   </span><span style="color: #0000BB">$ret </span><span style="color: #007700">= </span><span style="color: #0000BB">ftp_nb_continue</span><span style="color: #007700">(</span><span style="color: #0000BB">$ftp</span><span style="color: #007700">);<br />}<br />if (</span><span style="color: #0000BB">$ret </span><span style="color: #007700">!= </span><span style="color: #0000BB">FTP_FINISHED</span><span style="color: #007700">) {<br />   echo </span><span style="color: #DD0000">"下载文件出错..."</span><span style="color: #007700">;<br />   exit(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-3">
    <p><strong>示例 #3 
      使用 <span class="function"><strong>ftp_nb_get()</strong></span> 从指定位置恢复下载文件
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">// 禁止 Autoseek<br /></span><span style="color: #0000BB">ftp_set_option</span><span style="color: #007700">(</span><span style="color: #0000BB">$ftp</span><span style="color: #007700">, </span><span style="color: #0000BB">FTP_AUTOSEEK</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// 初始化<br /></span><span style="color: #0000BB">$ret </span><span style="color: #007700">= </span><span style="color: #0000BB">ftp_nb_get</span><span style="color: #007700">(</span><span style="color: #0000BB">$ftp</span><span style="color: #007700">, </span><span style="color: #DD0000">"newfile"</span><span style="color: #007700">, </span><span style="color: #DD0000">"README"</span><span style="color: #007700">, </span><span style="color: #0000BB">FTP_BINARY</span><span style="color: #007700">, </span><span style="color: #0000BB">100</span><span style="color: #007700">);<br />while (</span><span style="color: #0000BB">$ret </span><span style="color: #007700">== </span><span style="color: #0000BB">FTP_MOREDATA</span><span style="color: #007700">) {<br /><br />   </span><span style="color: #FF8000">/* ... */<br />   <br />   // 继续下载...<br />   </span><span style="color: #0000BB">$ret </span><span style="color: #007700">= </span><span style="color: #0000BB">ftp_nb_continue</span><span style="color: #007700">(</span><span style="color: #0000BB">$ftp</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
    在上面的示例中，<var class="filename">newfile</var> 会比 FTP 服务器上的
    <var class="filename">README</var> 文件小 100 bytes，因为开始下载的时候设置了偏移量为 100。如果我们不禁止
    <strong><code><a href="ftp.constants.php#constant.ftp-autoseek">FTP_AUTOSEEK</a></code></strong>，则 <var class="filename">newfile</var> 文件前面的 100 bytes 会变成
    <code class="literal">&#039;\0&#039;</code>。
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.ftp-nb-get-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.ftp-nb-fget.php" class="function" rel="rdfs-seeAlso">ftp_nb_fget()</a> - 从 FTP 服务器获取文件并写入到一个打开的文件（非阻塞）</span></li>
    <li><span class="function"><a href="function.ftp-nb-continue.php" class="function" rel="rdfs-seeAlso">ftp_nb_continue()</a> - 连续获取／发送文件（以不分块的方式 non-blocking）</span></li>
    <li><span class="function"><a href="function.ftp-fget.php" class="function" rel="rdfs-seeAlso">ftp_fget()</a> - 从 FTP 服务器上下载文件并保存到本地已打开的文件中</span></li>
    <li><span class="function"><a href="function.ftp-get.php" class="function" rel="rdfs-seeAlso">ftp_get()</a> - 从 FTP 服务器上下载文件</span></li>
   </ul>
  </p>
 </div>

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