<!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>List files and directories inside the specified path</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.rewinddir.html">rewinddir</a></div> <div class="next" style="text-align: right; float: right;"><a href="book.fileinfo.html">Fileinfo</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.scandir" class="refentry"> <div class="refnamediv"> <h1 class="refname">scandir</h1> <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">scandir</span> — <span class="dc-title">List files and directories inside the specified path</span></p> </div> <div class="refsect1 description" id="refsect1-function.scandir-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>scandir</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$directory</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$sorting_order</code><span class="initializer"> = SCANDIR_SORT_ASCENDING</span></span> [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></span> ]] )</div> <p class="para rdfs-comment"> Returns an <span class="type"><a href="language.types.array.html" class="type array">array</a></span> of files and directories from the <em><code class="parameter">directory</code></em>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.scandir-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">directory</code></em></span> <dd> <p class="para"> The directory that will be scanned. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">sorting_order</code></em></span> <dd> <p class="para"> By default, the sorted order is alphabetical in ascending order. If the optional <em><code class="parameter">sorting_order</code></em> is set to <strong><code>SCANDIR_SORT_DESCENDING</code></strong>, then the sort order is alphabetical in descending order. If it is set to <strong><code>SCANDIR_SORT_NONE</code></strong> then the result is unsorted. </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.scandir-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an <span class="type"><a href="language.types.array.html" class="type array">array</a></span> of filenames on success, or <strong><code>FALSE</code></strong> on failure. If <em><code class="parameter">directory</code></em> is not a directory, then boolean <strong><code>FALSE</code></strong> is returned, and an error of level <strong><code>E_WARNING</code></strong> is generated. </p> </div> <div class="refsect1 changelog" id="refsect1-function.scandir-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.4.0</td> <td> <em><code class="parameter">sorting_order</code></em> <a href="dir.constants.html" class="link">constants</a> were added. Any nonzero value caused descending order in previous versions. So for all PHP versions, use <em>0</em> for ascending order, and <em>1</em> for descending order. An option for <strong><code>SCANDIR_SORT_NONE</code></strong> behavior did not exist prior to PHP 5.4.0. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.scandir-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2341"> <p><strong>Example #1 A simple <span class="function"><strong>scandir()</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">'/tmp'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$files1 </span><span style="color: #007700">= </span><span style="color: #0000BB">scandir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$files2 </span><span style="color: #007700">= </span><span style="color: #0000BB">scandir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$files1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$files2</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> Array ( [0] => . [1] => .. [2] => bar.php [3] => foo.txt [4] => somedir ) Array ( [0] => somedir [1] => foo.txt [2] => bar.php [3] => .. [4] => . ) </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-2342"> <p><strong>Example #2 PHP 4 alternatives to <span class="function"><strong>scandir()</strong></span></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">"/tmp"</span><span style="color: #007700">;<br /></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">false </span><span style="color: #007700">!== (</span><span style="color: #0000BB">$filename </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">))) {<br /> </span><span style="color: #0000BB">$files</span><span style="color: #007700">[] = </span><span style="color: #0000BB">$filename</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">sort</span><span style="color: #007700">(</span><span style="color: #0000BB">$files</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$files</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">rsort</span><span style="color: #007700">(</span><span style="color: #0000BB">$files</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$files</span><span style="color: #007700">);<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> Array ( [0] => . [1] => .. [2] => bar.php [3] => foo.txt [4] => somedir ) Array ( [0] => somedir [1] => foo.txt [2] => bar.php [3] => .. [4] => . ) </pre></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.scandir-notes"> <h3 class="title">Notes</h3> <div class="tip"><strong class="tip">Tip</strong><p class="simpara">A URL can be used as a filename with this function if the <a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">fopen wrappers</a> have been enabled. See <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> for more details on how to specify the filename. See the <a href="wrappers.html" class="xref">Supported Protocols and Wrappers</a> for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.</p></div> </div> <div class="refsect1 seealso" id="refsect1-function.scandir-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <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.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.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.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.sort.html" class="function" rel="rdfs-seeAlso">sort()</a> - Sort an array</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.rewinddir.html">rewinddir</a></div> <div class="next" style="text-align: right; float: right;"><a href="book.fileinfo.html">Fileinfo</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>