Creating a directory listing

From: Date: Wed, 02 Apr 2003 17:22:06 +0000
Subject: Creating a directory listing
Groups: php.db 
Request: Send a blank email to php-db+get-26816@lists.php.net to get a copy of this message
I'm trying to do this for a web site. I need to list the contents of each directory on a site to perhaps 4 levels deep (only html files). I've almost got a function going where it starts at the top level and checks each file to see if IT is a directory and if so chdir() to that and list the contents of it until it comes across a directory at that level then chdir() again and so on and when done it will have gone through all files in the 4 levels. I also have to consider searching the files in each directory for a search term (I've got the searching part OK). What is the most efficient way to go through a hierarchy of directories to list or check each file and subdirectory? Right now, this is what I have and I think it works, but not sure if it's most efficient way: if ($handle1 = @opendir($basedir)) {
    $dir0 = getcwd();
    while (false !== ($file1 = readdir($handle1))) {
        //ignore files that start with periods (hidden files and parent directories)
        if (substr($file1,0,1) != "." && substr($file1,0,2) != "..") {
            if (is_dir($file1)) {
                chdir($file1);
                $dir1 = getcwd();
                if ($handle2 = @opendir($dir1)) {
                    while (false !== ($file2 = readdir($handle2))) {
                        if (substr($file2,0,1) != "." && substr($file2,0,2) != "..") {
                            if (is_dir($file2)) {
                                chdir($file2);
                                $dir2 = getcwd();
// and so on as many levels as needed
                            } elseif (ereg("\.htm",$file2)) {
                                echo "$file2<br>";
                            }
                        }
                    }  //end while
                    closedir($handle2);
                    chdir($dir0);
                }  //end opendir
            } elseif (ereg("\.htm",$file1)) {
                echo "$file1<br>";
            }
        }
    }
    closedir($handle1);
    // output the results
} Thanks for any assistance. Dave

« previous php.db (#26816) next »