> php教程 > php手册 > php遍历一个文件夹下的所有目录及文件

php遍历一个文件夹下的所有目录及文件

WBOY
풀어 주다: 2016-06-13 10:38:22
원래의
1158명이 탐색했습니다.

  在面试中我们经常遇到这个题目:php遍历一个文件夹下的所有文件和子文件夹。

  这个题目有好多种解决方法。但大致思路都一样。采用递归。

<ol class="dp-c">
<li class="alt"><span><span> </span><span class="vars">$path</span><span> = </span><span class="string">./filepath</span><span>;  </span></span></li>
<li>
<span> </span><span class="keyword">function</span><span> getfiles(</span><span class="vars">$path</span><span>)  </span>
</li>
<li class="alt"><span> {  </span></li>
<li>
<span>     </span><span class="keyword">if</span><span>(!</span><span class="func">is_dir</span><span>(</span><span class="vars">$path</span><span>)) </span><span class="keyword">return</span><span>;  </span>
</li>
<li class="alt">
<span>    </span><span class="vars">$handle</span><span>  = opendir(</span><span class="vars">$path</span><span>);  </span>
</li>
<li>
<span>    </span><span class="keyword">while</span><span>( false !== (</span><span class="vars">$file</span><span> = readdir(</span><span class="vars">$handle</span><span>)))  </span>
</li>
<li class="alt"><span>    {  </span></li>
<li>
<span>        </span><span class="keyword">if</span><span>(</span><span class="vars">$file</span><span> != </span><span class="string">.</span><span>  &&  </span><span class="vars">$file</span><span>!=</span><span class="string">..</span><span>)  </span>
</li>
<li class="alt"><span>        {  </span></li>
<li>
<span>            </span><span class="vars">$path2</span><span>= </span><span class="vars">$path</span><span>.</span><span class="string">/</span><span>.</span><span class="vars">$file</span><span>;  </span>
</li>
<li class="alt">
<span>            </span><span class="keyword">if</span><span>(</span><span class="func">is_dir</span><span>(</span><span class="vars">$path2</span><span>))  </span>
</li>
<li><span>            {  </span></li>
<li class="alt">
<span>                </span><span class="func">echo</span><span>   </span>
</li>
<li><span>;  </span></li>
<li class="alt">
<span>                </span><span class="func">echo</span><span> </span><span class="vars">$file</span><span>;  </span>
</li>
<li>
<span>               getfiles(</span><span class="vars">$path2</span><span>);  </span>
</li>
<li class="alt">
<span>            }</span><span class="keyword">else</span><span> </span>
</li>
<li><span>            {  </span></li>
<li class="alt">
<span>               </span><span class="func">echo</span><span>   </span>
</li>
<li><span>;  </span></li>
<li class="alt">
<span>                </span><span class="func">echo</span><span> </span><span class="vars">$file</span><span>;  </span>
</li>
<li><span>            }  </span></li>
<li class="alt"><span>        }  </span></li>
<li><span>    }  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt">
<span>  print_r( getfiles(</span><span class="vars">$path</span><span>));  </span>
</li>
<li><span> </span></li>
<li class="alt">
<span class="func">echo</span><span>   </span>
</li>
<li><span><hr>;  </span></li>
<li class="alt"><span> </span></li>
<li>
<span class="keyword">function</span><span> getdir(</span><span class="vars">$path</span><span>)  </span>
</li>
<li class="alt"><span>{  </span></li>
<li>
<span>    </span><span class="keyword">if</span><span>(!</span><span class="func">is_dir</span><span>(</span><span class="vars">$path</span><span>)) </span><span class="keyword">return</span><span>;  </span>
</li>
<li class="alt">
<span>    </span><span class="vars">$handle</span><span> = dir(</span><span class="vars">$path</span><span>);  </span>
</li>
<li>
<span>    </span><span class="keyword">while</span><span>(</span><span class="vars">$file</span><span>=</span><span class="vars">$handle</span><span>->read())  </span>
</li>
<li class="alt"><span>    {  </span></li>
<li>
<span>        </span><span class="keyword">if</span><span>(</span><span class="vars">$file</span><span>!=</span><span class="string">.</span><span> && </span><span class="vars">$file</span><span>!=</span><span class="string">..</span><span>)  </span>
</li>
<li class="alt"><span>        {  </span></li>
<li>
<span>            </span><span class="vars">$path2</span><span> = </span><span class="vars">$path</span><span>.</span><span class="string">/</span><span>.</span><span class="vars">$file</span><span>;  </span>
</li>
<li class="alt">
<span>            </span><span class="keyword">if</span><span>(</span><span class="func">is_dir</span><span>(</span><span class="vars">$path2</span><span>))  </span>
</li>
<li><span>            {  </span></li>
<li class="alt">
<span>                    </span><span class="func">echo</span><span> </span><span class="vars">$file</span><span>.</span><span class="string">"	"</span><span>;  </span>
</li>
<li>
<span>                     getdir(</span><span class="vars">$path2</span><span>);  </span>
</li>
<li class="alt">
<span>            }</span><span class="keyword">else</span><span> </span>
</li>
<li><span>            {  </span></li>
<li class="alt">
<span>                </span><span class="func">echo</span><span> </span><span class="vars">$file</span><span>.  </span>
</li>
<li><span>;  </span></li>
<li class="alt"><span>            }  </span></li>
<li><span>        }  </span></li>
<li class="alt"><span>    }  </span></li>
<li><span>}  </span></li>
<li class="alt">
<span> getdir(</span><span class="vars">$path</span><span>);  </span>
</li>
<li><span> </span></li>
<li class="alt">
<span> </span><span class="func">echo</span><span>   </span>
</li>
<li><span><hr>;  </span></li>
<li class="alt"><span> </span></li>
<li>
<span> </span><span class="keyword">function</span><span> get_dir_scandir(</span><span class="</p">
</span>
</li>
</ol>
로그인 후 복사
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿