Source of: /manual/en/function.readdir.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = 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 => 'en',
),
'this' =>
array (
0 => 'function.readdir.php',
1 => 'readdir',
),
'up' =>
array (
0 => 'ref.dir.php',
1 => 'Directory Functions',
),
'prev' =>
array (
0 => 'function.opendir.php',
1 => 'opendir',
),
'next' =>
array (
0 => 'function.rewinddir.php',
1 => 'rewinddir',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="function.readdir" class="refentry">
<div class="refnamediv">
<h1 class="refname">readdir</h1>
<p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">readdir</span> — <span class="dc-title">Read entry from directory handle</span></p>
</div>
<a name="function.readdir.description"></a><div class="refsect1 description">
<h3 class="title">Description</h3>
<div class="methodsynopsis dc-description">
<span class="type">string</span> <span class="methodname"><b>readdir</b></span>
([ <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$dir_handle</tt></span>
] )</div>
<p class="para rdfs-comment">
Returns the filename of the next file from the directory. The
filenames are returned in the order in which they are stored by
the filesystem.
</p>
</div>
<a name="function.readdir.parameters"></a><div class="refsect1 parameters">
<h3 class="title">Parameters</h3>
<p class="para">
</p><dl>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">dir_handle</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
The directory handle <a href="language.types.resource.php" class="type resource">resource</a> previously opened
with <a href="function.opendir.php" class="function">opendir()</a>. If the directory handle is
not specified, the last link opened by <a href="function.opendir.php" class="function">opendir()</a>
is assumed.
</p>
</dd>
</dl>
<p>
</p>
</div>
<a name="function.readdir.returnvalues"></a><div class="refsect1 returnvalues">
<h3 class="title">Return Values</h3>
<p class="para">
Returns the filename on success or <b><tt class="constant">FALSE</tt></b> on failure.
</p>
<div class="warning"><b class="warning">Warning</b><p class="simpara">This function may
return Boolean <b><tt class="constant">FALSE</tt></b>, but may also return a non-Boolean value which
evaluates to <b><tt class="constant">FALSE</tt></b>, such as <i>0</i> or
"". Please read the section on <a href="language.types.boolean.php" class="link">Booleans</a> for more
information. Use <a href="language.operators.comparison.php" class="link">the ===
operator</a> for testing the return value of this
function.</p></div>
</div>
<a name="function.readdir.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example">
<p><b>Example #1 List all files in a directory</b></p>
<div class="example-contents para"><p>
Please note the fashion in which <b>readdir()</b>'s
return value is checked in the examples 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.php" class="link">Comparison
Operators</a> for more information) <b><tt class="constant">FALSE</tt></b> since otherwise,
any directory entry whose name evaluates to <b><tt class="constant">FALSE</tt></b> will stop the
loop (e.g. a directory named "0").
</p></div>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Note that !== did not exist until 4.0.0-RC2<br /><br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">opendir</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/files'</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"Directory handle: </span><span style="color: #0000BB">$handle</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"Files:\n"</span><span style="color: #007700">;<br /><br /> </span><span style="color: #FF8000">/* This is the correct way to loop over the directory. */<br /> </span><span style="color: #007700">while (</span><span style="color: #0000BB">false </span><span style="color: #007700">!== (</span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">readdir</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">))) {<br /> echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$file</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /> }<br /><br /> </span><span style="color: #FF8000">/* This is the WRONG way to loop over the directory. */<br /> </span><span style="color: #007700">while (</span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">readdir</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$file</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /> }<br /><br /> </span><span style="color: #0000BB">closedir</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #2
List all files in the current directory and strip out <i>.</i>
and <i>..</i>
</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">opendir</span><span style="color: #007700">(</span><span style="color: #DD0000">'.'</span><span style="color: #007700">)) {<br /> while (</span><span style="color: #0000BB">false </span><span style="color: #007700">!== (</span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">readdir</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">))) {<br /> if (</span><span style="color: #0000BB">$file </span><span style="color: #007700">!= </span><span style="color: #DD0000">"." </span><span style="color: #007700">&& </span><span style="color: #0000BB">$file </span><span style="color: #007700">!= </span><span style="color: #DD0000">".."</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$file</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /> }<br /> }<br /> </span><span style="color: #0000BB">closedir</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
</div>
<a name="function.readdir.seealso"></a><div class="refsect1 seealso">
<h3 class="title">See Also</h3>
<p class="para">
</p><ul class="simplelist">
<li class="member"><a href="function.is-dir.php" class="function" rel="rdfs-seeAlso">is_dir()</a> - Tells whether the filename is a directory</li>
<li class="member"><a href="function.glob.php" class="function" rel="rdfs-seeAlso">glob()</a> - Find pathnames matching a pattern</li>
<li class="member"><a href="function.opendir.php" class="function" rel="rdfs-seeAlso">opendir()</a> - Open directory handle</li>
<li class="member"><a href="function.scandir.php" class="function" rel="rdfs-seeAlso">scandir()</a> - List files and directories inside the specified path</li>
</ul><p>
</p>
</div>
</div><?php manual_footer(); ?>