php 遍历目录文件方法

来源:互联网 发布:linux 发送文件 编辑:程序博客网 时间:2024/05/29 10:58
<?php/**********************一个简单的目录递归函数第一种实现办法:用dir返回对象***********************/function tree($directory) {     if(is_dir($directory)) {        //返回一个 Directory 类实例        $mydir = dir($directory);        echo "<ul>\n";        //从目录句柄中读取条目        while($file = $mydir->read()) {            if(is_dir("$directory/$file") && $file != "." && $file != "..") {                echo "<li><font color=\"#ff00cc\"><b>$file</b></font></li>\n";                //递归读取目录                 tree("$directory/$file");            } elseif ($file != "." && $file != "..") {                echo "<li>$file</li>\n";            }        }        echo "</ul>\n";        // 释放目录句柄        $mydir->close();    } else {        echo $directory . '<br>';    }} //开始运行echo "<h2>目录为粉红色</h2><br>\n"; tree("../www.test.com"); 
<?php /***********************第二种实现办法:用readdir()函数************************/function listDir($dir) {    if(is_dir($dir)) {        //打开目录句柄        if ($dh = opendir($dir)) {            //从目录句柄中读取条目            while (($file = readdir($dh)) !== false) {                if(is_dir($dir."/".$file) && $file!="." && $file!="..") {                    echo "<b><font color='red'>文件名:</font></b>",$file,"<br><hr>";                    listDir($dir."/".$file."/");                }                else {                    if($file != "." && $file != "..") {                        echo $file."<br>";                    }                }            }            closedir($dh);        }    } else {        echo $dir . '<br>';    }}//开始运行listDir("../www.test.com");?>

以数组形式返回整个文件夹中多重文件

<?php/**     * Create a Directory Map     *     * Reads the specified directory and builds an array     * representation of it. Sub-folders contained with the     * directory will be mapped as well.     *     * @param   string  $source_dir        Path to source     * @param   int $directory_depth   Depth of directories to traverse     *                      (0 = fully recursive, 1 = current dir, etc)     * @param   bool    $hidden            Whether to show hidden files     * @return  array     */function directory_map($source_dir, $directory_depth = 0, $hidden = FALSE)    {        if ($fp = @opendir($source_dir))        {            $filedata  = array();            $new_depth= $directory_depth - 1;            $source_dir= rtrim($source_dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;            while (FALSE !== ($file = readdir($fp)))            {                // Remove '.', '..', and hidden files [optional]                if ($file === '.' OR $file === '..' OR ($hidden === FALSE && $file[0] === '.'))                {                    continue;                }                is_dir($source_dir.$file) && $file .= DIRECTORY_SEPARATOR;                if (($directory_depth < 1 OR $new_depth > 0) && is_dir($source_dir.$file))                {                    $filedata[$file] = directory_map($source_dir.$file, $new_depth, $hidden);                }                else                {                    $filedata[] = $file;                }            }            closedir($fp);            return $filedata;        }        return FALSE;    }    $aa = directory_map('../www.test.com');    var_dump($aa);
0 0