<!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>Read entry from directory handle</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.opendir.html">opendir</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.rewinddir.html">rewinddir</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.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> <div class="refsect1 description" id="refsect1-function.readdir-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>readdir</strong></span> ([ <span class="methodparam"><span class="type">resource</span> <code class="parameter">$dir_handle</code></span> ] )</div> <p class="para rdfs-comment"> Returns the name of the next entry in the directory. The entries are returned in the order in which they are stored by the filesystem. </p> </div> <div class="refsect1 parameters" id="refsect1-function.readdir-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">dir_handle</code></em></span> <dd> <p class="para"> The directory handle <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> previously opened with <span class="function"><a href="function.opendir.html" class="function">opendir()</a></span>. If the directory handle is not specified, the last link opened by <span class="function"><a href="function.opendir.html" class="function">opendir()</a></span> is assumed. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.readdir-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the entry name on success or <strong><code>FALSE</code></strong> on failure. </p> <div class="warning"><strong class="warning">Warning</strong><p class="simpara">This function may return Boolean <strong><code>FALSE</code></strong>, but may also return a non-Boolean value which evaluates to <strong><code>FALSE</code></strong>. Please read the section on <a href="language.types.boolean.html" class="link">Booleans</a> for more information. Use <a href="language.operators.comparison.html" class="link">the === operator</a> for testing the return value of this function.</p></div> </div> <div class="refsect1 examples" id="refsect1-function.readdir-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2339"> <p><strong>Example #1 List all entries in a directory</strong></p> <div class="example-contents"><p> Please note the fashion in which <span class="function"><strong>readdir()</strong></span>'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.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 (e.g. a directory named "0"). </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<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">"Entries:\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">$entry </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">$entry</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">$entry </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">$entry</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 class="para"> <div class="example" id="example-2340"> <p><strong>Example #2 List all entries in the current directory and strip out <em>.</em> and <em>..</em> </strong></p> <div class="example-contents"> <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">$entry </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">$entry </span><span style="color: #007700">!= </span><span style="color: #DD0000">"." </span><span style="color: #007700">&& </span><span style="color: #0000BB">$entry </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">$entry</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> </div> <div class="refsect1 seealso" id="refsect1-function.readdir-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.is-dir.html" class="function" rel="rdfs-seeAlso">is_dir()</a> - Tells whether the filename is a directory</span></li> <li class="member"> <span class="function"><a href="function.glob.html" class="function" rel="rdfs-seeAlso">glob()</a> - Find pathnames matching a pattern</span></li> <li class="member"> <span class="function"><a href="function.opendir.html" class="function" rel="rdfs-seeAlso">opendir()</a> - Open directory handle</span></li> <li class="member"> <span class="function"><a href="function.scandir.html" class="function" rel="rdfs-seeAlso">scandir()</a> - List files and directories inside the specified path</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.opendir.html">opendir</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.rewinddir.html">rewinddir</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>