次のコードに示すように、PHP は指定されたフォルダー内のすべてのファイルをスキャンします。
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li><span>session_start(); </span></li><li class="alt"><span>if($<span class="attribute">Submit</span><span>=="提交"){ </span></span></li><li><span> $<span class="attribute">shili</span><span>=$file_dir; </span></span></li><li class="alt"><span>} </span></li><li><span><span class="tag">?></span><span> </span></span></li> <li class="alt"><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li> <li><span><span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span><span class="tag"><</span><span class="tag-name">meta</span><span> </span><span class="attribute">http-equiv</span><span>=</span><span class="attribute-value">"Content-Type"</span><span> </span><span class="attribute">content</span><span>=</span><span class="attribute-value">"text/html; charset=gb2312"</span><span class="tag">></span><span> </span></span></li> <li><span><span class="tag"><</span><span class="tag-name">title</span><span class="tag">></span><span>遍历指定目录下的所有文件 - www.cxybl.com</span><span class="tag"></</span><span class="tag-name">title</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span><span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li> <li><span><span class="tag"><</span><span class="tag-name">table</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"350"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"150"</span><span> </span><span class="attribute">border</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">cellpadding</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">cellspacing</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">background</span><span>=</span><span class="attribute-value">"images/324.jpg"</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"><</span><span class="tag-name">form</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"form1"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"post"</span><span> </span><span class="attribute">action</span><span>=</span><span class="attribute-value">"index.php"</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"132"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"55"</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"135"</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"83"</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"40"</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"file_dir"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text"</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"file_dir"</span><span> </span><span class="attribute">size</span><span>=</span><span class="attribute-value">"18"</span><span> </span><span class="attribute">maxlength</span><span>=</span><span class="attribute-value">"50"</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"><</span><span class="tag-name">td</span><span class="tag">></span><span> </span><span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"submit"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"Submit"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"提交"</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"55"</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"><</span><span class="tag-name">td</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"><</span><span class="tag-name">td</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span> <span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li> <li><span> <span class="tag"></</span><span class="tag-name">form</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span><span class="tag"></</span><span class="tag-name">table</span><span class="tag">></span><span> </span></span></li> <li><span><span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li class="alt"><span> $<span class="attribute">shili</span><span> = $file_dir ; </span></span></li><li><span> if ( !file_exists ( $shili )){ </span></li><li class="alt"><span> print $shili."目录不存在!" ; </span></li><li><span> }else{ </span></li><li class="alt"><span> $<span class="attribute">i</span><span> = </span><span class="attribute-value">0</span><span>; </span></span></li><li><span> if ( is_dir ( $shili )){ //检测是否是合法目录 </span></li><li class="alt"><span> if ($<span class="attribute">shi</span><span> = </span><span class="attribute-value">opendir</span><span> ( $shili )){ //打开目录 </span></span></li><li><span> while ($<span class="attribute">li</span><span> = </span><span class="attribute-value">readdir</span><span>( $shi )){ //读取目录 </span></span></li><li class="alt"><span> $i++ ; </span></li><li><span> echo " $i: $li <span class="tag"><</span><span class="tag-name">br</span><span class="tag">></span><span> " ; } } } //输出目录中的内容 </span></span></li> <li class="alt"><span> closedir ( $shi ) ; } <span class="tag">?></span><span> </span></span></li> <li><span><span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span><span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li> </ol>