function.readdir.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 134 行
HTML
134 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Read entry from directory handle</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div 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"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><b><b>readdir</b></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> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">dir_handle</tt></i></span> <dd> <p class="para"> The directory handle <a href="language.types.resource.html" class="type resource">resource</a> previously opened with <a href="function.opendir.html" class="function">opendir()</a>. If the directory handle is not specified, the last link opened by <a href="function.opendir.html" class="function">opendir()</a> is assumed. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the filename on success, or <b><tt>FALSE</tt></b> on failure. </p> <div class="warning"><b class="warning">Warning</b><p class="simpara">This function mayreturn Boolean <b><tt>FALSE</tt></b>, but may also return a non-Boolean value whichevaluates to <b><tt>FALSE</tt></b>, such as <i>0</i> or"". Please read the section on <a href="language.types.boolean.html" class="link">Booleans</a> for moreinformation. Use <a href="language.operators.comparison.html" class="link">the ===operator</a> for testing the return value of thisfunction.</p></div> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 List all files in a directory</b></p> <div class="example-contents"><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.html" class="link">Comparison Operators</a> for more information) <b><tt>FALSE</tt></b> since otherwise, any directory entry whose name evaluates to <b><tt>FALSE</tt></b> 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 /></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: $handle\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">"$file\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">"$file\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"> <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"><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">"$file\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"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.is-dir.html" class="function" rel="rdfs-seeAlso">is_dir()</a></li> <li class="member"><a href="function.glob.html" class="function" rel="rdfs-seeAlso">glob()</a></li> </ul> </p> </div></div><hr /><div 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>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?