Heim > Backend-Entwicklung > PHP-Tutorial > php遍历一个文件夹下的所有目录及文件_PHP教程

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

WBOY
Freigeben: 2016-07-13 17:39:32
Original
830 Leute haben es durchsucht

  在面试中我们经常遇到这个题目: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">
<p align="left"></p>
<div style="display:none;">
<span id="url" itemprop="url">http://www.bkjia.com/PHPjc/486278.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/486278.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">在面试中我们经常遇到这个题目:php遍历一个文件夹下的所有文件和子文件夹。 这个题目有好多种解决方法。但大致思路都一样。采用递归...</span>
</div>
<div class="art_confoot"></div>
</span>
</li>
</ol>
Nach dem Login kopieren
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage