<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Return an instance of the Directory class</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.closedir.html">closedir</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.getcwd.html">getcwd</a></div> <div class="up"><a href="ref.dir.html">Directory Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.dir" class="refentry"> <div class="refnamediv"> <h1 class="refname">dir</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">dir</span> — <span class="dc-title">Return an instance of the Directory class</span></p> </div> <div class="refsect1 description" id="refsect1-function.dir-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="class.directory.html" class="type Directory">Directory</a></span> <span class="methodname"><strong>dir</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$directory</code></span> [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></span> ] )</div> <p class="para rdfs-comment"> A pseudo-object oriented mechanism for reading a directory. The given <em><code class="parameter">directory</code></em> is opened. </p> </div> <div class="refsect1 parameters" id="refsect1-function.dir-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <span class="term"><em><code class="parameter">directory</code></em></span> <dd> <p class="para"> Directory to open </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">context</code></em></span> <dd> <p class="para"> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">Context support was added with PHP 5.0.0. For a description of <em>contexts</em>, refer to <a href="book.stream.html" class="xref">Streams</a>.</span></p></blockquote> </p> </dd> </dt> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-function.dir-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an instance of <a href="class.directory.html" class="classname">Directory</a>, or <strong><code>NULL</code></strong> with wrong parameters, or <strong><code>FALSE</code></strong> in case of another error. </p> </div> <div class="refsect1 examples" id="refsect1-function.dir-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2336"> <p><strong>Example #1 <span class="function"><strong>dir()</strong></span> example</strong></p> <div class="example-contents"><p> Please note the fashion in which <span class="function"><a href="directory.read.html" class="function">Directory::read()</a></span>'s return value is checked in the example below. We are explicitly testing whether the return value is identical to (equal to and of the same type as - see <a href="language.operators.comparison.html" class="link"> Comparison Operators</a> for more information) <strong><code>FALSE</code></strong> since otherwise, any directory entry whose name evaluates to <strong><code>FALSE</code></strong> will stop the loop. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?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">-></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">-></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">-></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">-></span><span style="color: #0000BB">close</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 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">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The order in which directory entries are returned by the read method is system-dependent. </p> </p></blockquote> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.closedir.html">closedir</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.getcwd.html">getcwd</a></div> <div class="up"><a href="ref.dir.html">Directory Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>