<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.dir.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.dir.php',
    1 => 'dir',
    2 => '返回一个 Directory 类实例',
  ),
  'up' => 
  array (
    0 => 'ref.dir.php',
    1 => '目录函数函数',
  ),
  'prev' => 
  array (
    0 => 'function.closedir.php',
    1 => 'closedir',
  ),
  'next' => 
  array (
    0 => 'function.getcwd.php',
    1 => 'getcwd',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/dir/functions/dir.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.dir" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">dir</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">dir</span> &mdash; <span class="dc-title">返回一个 Directory 类实例</span></p>

 </div>

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

  
  <p class="para rdfs-comment">
   以面向对象的方式访问目录。打开 
   <code class="parameter">directory</code> 参数指定的目录。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.dir-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt><code class="parameter">directory</code></dt>
    <dd>
     <p class="para">
      被打开的目录
     </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>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.dir-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回 <span class="classname"><a href="class.directory.php" class="classname">Directory</a></span> 的实例，出现错误时返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.dir-changelog">
  <h3 class="title">更新日志</h3>
  <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">context</code> 现在可为空（nullable）。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.dir-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>dir()</strong></span> 示例</strong></p>
    <div class="example-contents"><p>
     请特别注意下面示例中 <span class="function"><a href="directory.read.php" class="function">Directory::read()</a></span> 函数返回值的判断方式。
     我们严格测试（值相等，并且类型相同，请参考 <a href="language.operators.comparison.php" class="link">
     比较运算符</a> ）返回值等于 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> ，因为有些情况下，目录名可能&quot;等于&quot; <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> ，导致
     跳出循环。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$d </span><span style="color: #007700">= </span><span style="color: #0000BB">dir</span><span style="color: #007700">(</span><span style="color: #DD0000">"/etc/php5"</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Handle: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$d</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">handle </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Path: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$d</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">path </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />while (</span><span style="color: #0000BB">false </span><span style="color: #007700">!== (</span><span style="color: #0000BB">$entry </span><span style="color: #007700">= </span><span style="color: #0000BB">$d</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">read</span><span style="color: #007700">())) {<br />   echo </span><span style="color: #0000BB">$entry</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">$d</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">close</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">Handle: Resource id #2
Path: /etc/php5
.
..
apache
cgi
cli</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.dir-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    目录条目返回的顺序依赖于系统。
   </p>
  </p></blockquote>
 </div>

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