教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php读出目录下的所有目录及子目录下文件

php读出目录下的所有目录及子目录下文件

发布时间:2016-10-27   编辑:jiaochengji.com
教程集为您提供php读出目录下的所有目录及子目录下文件等资源,欢迎您收藏本站,我们将为您提供最新的php读出目录下的所有目录及子目录下文件资源
一个利用php读出目录下的所有目录及子目录下文件的代码,很简单方便的第一个就是读取一级目录,后面可以无限目录读取有需要的同学可以参考一下。
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2745')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2745>

<?opendir("abc")or die("打开目录不成功<br>");

readdir($dirname)."<br>";

readdir($dirname)."<br>";
while(($filess=readdir($dirname))!=false)

{
  if(is_dir("abc/".$filess))
 {
 
    echo "目录:".$filess."<br>";

    }
   else
 {
  
     echo "文件:".$filess."<br>";
   }

}
closedir($dirname);

?>

只能读出html文件名,读不出abc目录下的目录名,下面我们再看一实例

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4873')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4873>

<?php 
/*
*   递归获取指定路径下的所有文件或匹配指定正则的文件(不包括“.”和“..”),结果以数组形式返回
*   @param  string  $dir
*   @param  string  [$pattern]
*   @return array
*/ 
function file_list($dir,$pattern="") 

    $arr=array(); 
    $dir_handle=opendir($dir); 
    if($dir_handle) 
    { 
        // 这里必须严格比较,因为返回的文件名可能是“0”  
        while(($file=readdir($dir_handle))!==false) 
        { 
            if($file==='.' || $file==='..') 
            { 
                continue; 
            } 
            $tmp=realpath($dir.'/'.$file); 
            if(is_dir($tmp)) 
            { 
                $retArr=file_list($tmp,$pattern); 
                if(!emptyempty($retArr)) 
                { 
                    $arr[]=$retArr; 
                } 
            } 
            else 
            { 
                if($pattern==="" || preg_match($pattern,$tmp)) 
                { 
                    $arr[]=$tmp; 
                } 
            } 
        } 
        closedir($dir_handle); 
    } 
    return $arr; 

 
// 列出网站根目录下所有以".php"扩展名(不区分大小写)结尾的文件  
echo '<pre>'; 
print_r(file_list($_SERVER['DOCUMENT_ROOT'],"//.php$/i")); 
echo '</pre>'; 
?> 
这个就可以方便的无限目录遍历了。

您可能感兴趣的文章:
php 读取目录文件夹列表的例子
php目录遍历与删除的代码一例
php获取目录中所有文件名及判断文件与目录的方法
PHP中读取某个目录下的文件
php无限遍历目录代码
用PHP实现遍历删除目录及此目录下存放的所有文件
删除指定文件夹中所有文件的php代码
php列出目录下所有文件的代码举例
关于PHP目录操作总结
PHP删除N分钟前创建的所有文件的小例子

[关闭]
~ ~