> php教程 > php手册 > PHP遍历指定文件夹下的所有文件

PHP遍历指定文件夹下的所有文件

WBOY
풀어 주다: 2016-06-21 08:52:41
원래의
1185명이 탐색했습니다.

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 class="tag-name">html</span><span class="tag">></span><span> </span></span></span></li>
<li><span><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span><span class="tag"><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></span></li>
<li><span><span class="tag"><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></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 class="tag-name">body</span><span class="tag">></span><span> </span></span></span></li>
<li><span><span class="tag"><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></span></li>
<li class="alt"><span>  <span class="tag"><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></span></li>
<li><span>  <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>    <span class="tag"><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></span></li>
<li><span>    <span class="tag"><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></span></li>
<li class="alt"><span>    <span class="tag"><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></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 class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"><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></span></li>
<li class="alt"><span>    <span class="tag"><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></span></li>
<li><span>     <span class="tag"><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></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">td</span><span class="tag">></span><span> </span><span class="tag"><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></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 class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"><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></span></li>
<li class="alt"><span>    <span class="tag"><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></span></li>
<li><span>    <span class="tag"><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></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 class="tag-name">br</span><span class="tag">></span><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>
로그인 후 복사



관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿