<!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>Open directory handle</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.getcwd.html">getcwd</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.readdir.html">readdir</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.opendir" class="refentry"> <div class="refnamediv"> <h1 class="refname">opendir</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">opendir</span> — <span class="dc-title">Open directory handle</span></p> </div> <div class="refsect1 description" id="refsect1-function.opendir-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><strong>opendir</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$path</code></span> [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></span> ] )</div> <p class="para rdfs-comment"> Opens up a directory handle to be used in subsequent <span class="function"><a href="function.closedir.html" class="function">closedir()</a></span>, <span class="function"><a href="function.readdir.html" class="function">readdir()</a></span>, and <span class="function"><a href="function.rewinddir.html" class="function">rewinddir()</a></span> calls. </p> </div> <div class="refsect1 parameters" id="refsect1-function.opendir-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">path</code></em></span> <dd> <p class="para"> The directory path that is to be opened </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">context</code></em></span> <dd> <p class="para"> For a description of the <em><code class="parameter">context</code></em> parameter, refer to <a href="ref.stream.html" class="link">the streams section</a> of the manual. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.opendir-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns a directory handle <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> on success, or <strong><code>FALSE</code></strong> on failure. </p> <p class="para"> If <em><code class="parameter">path</code></em> is not a valid directory or the directory can not be opened due to permission restrictions or filesystem errors, <span class="function"><strong>opendir()</strong></span> returns <strong><code>FALSE</code></strong> and generates a PHP error of level <a href="errorfunc.constants.html" class="link"> E_WARNING</a>. You can suppress the error output of <span class="function"><strong>opendir()</strong></span> by prepending '<a href="language.operators.errorcontrol.html" class="link">@</a>' to the front of the function name. </p> </div> <div class="refsect1 changelog" id="refsect1-function.opendir-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.0.0</td> <td> <em><code class="parameter">path</code></em> supports the <em>ftp://</em> URL wrapper. </td> </tr> <tr> <td>4.3.0</td> <td> <em><code class="parameter">path</code></em> can also be any URL which supports directory listing, however only the <em>file://</em> URL wrapper supports this in PHP 4 </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.opendir-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2338"> <p><strong>Example #1 <span class="function"><strong>opendir()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$dir </span><span style="color: #007700">= </span><span style="color: #DD0000">"/etc/php5/"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Open a known directory, and proceed to read its contents<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">is_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir</span><span style="color: #007700">)) {<br /> if (</span><span style="color: #0000BB">$dh </span><span style="color: #007700">= </span><span style="color: #0000BB">opendir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir</span><span style="color: #007700">)) {<br /> 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">$dh</span><span style="color: #007700">)) !== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"filename: </span><span style="color: #0000BB">$file</span><span style="color: #DD0000"> : filetype: " </span><span style="color: #007700">. </span><span style="color: #0000BB">filetype</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir </span><span style="color: #007700">. </span><span style="color: #0000BB">$file</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> }<br /> </span><span style="color: #0000BB">closedir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dh</span><span style="color: #007700">);<br /> }<br />}<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> filename: . : filetype: dir filename: .. : filetype: dir filename: apache : filetype: dir filename: cgi : filetype: dir filename: cli : filetype: dir </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.opendir-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.readdir.html" class="function" rel="rdfs-seeAlso">readdir()</a> - Read entry from directory handle</span></li> <li class="member"> <span class="function"><a href="function.dir.html" class="function" rel="rdfs-seeAlso">dir()</a> - Return an instance of the Directory class</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.getcwd.html">getcwd</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.readdir.html">readdir</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>