首页 > PHP > 用PHP递归循环每一个目录

用PHP递归循环每一个目录

2009年03月31号
查看评论 发表评论 1,028次浏览

多时候我们要遍历一个文件夹里面的所有目录,列出里面所有的文件,PHP本身自带的有一个readdir的函数,不过只能读取当前的目录,根据这个函数,我写了另外一个函数,用来实现我的需求。函数的原理很简单,主要就是用了一下递归调用。function file_list($path) {
    if ($handle = opendir($path))  {
        while (false !== ($file = readdir($handle)))  {
            if ($file != “.” && $file != “..”) {
                if (is_dir($path.”/”.$file)) {
                   echo $path.”: “.$file.”<br>”;//去掉此行显示的是所有的非目录文件
                   file_list($path.”/”.$file);
                }  else  {
                   echo $path.”: “.$file.”<br>”;
                }
            }
        }
    }
}
这个函数还可以继续做一些改进,加入一些文件夹或文件的图标什么的,这样就可以做成更强大的一个函数了,有兴趣的朋友可以扩展的一下。

类别PHP 标签
  1. cheap designer handbags
    发表于 2010年06月18号 09时30分43秒 | 1楼

    I was wondering the same thing.

  2. mbt shoes
    发表于 2010年06月21号 15时19分06秒 | 2楼

    This is truly the 2010 version.

  3. louis vuitton epi leather wallet
    发表于 2010年06月26号 14时27分20秒 | 3楼

    I’m happy to be here and to learn with you, your comments, and I want to thank you for all that.

  4. Mulberry Handbags
    发表于 2010年06月27号 05时22分50秒 | 4楼

    Cheers!

  5. timberland boots
    发表于 2010年07月03号 17时29分29秒 | 5楼

    Cheers!

  6. timberland boots sale
    发表于 2010年07月16号 01时02分16秒 | 6楼

    I was thinking today that being grateful every moment is great freedom.

  7. puma online store
    发表于 2010年07月26号 11时24分22秒 | 7楼

    Thank you all for the wonderful comments.

  8. lv replica handbags
    发表于 2010年07月27号 14时22分58秒 | 8楼

    LOL!! Thanks for the laugh!

  1. 目前没有通告
你必需 登陆 才能发表评论.