Jump to content

Listing subdirectories and their contents


pcw

Recommended Posts

Hi,

 

I know how to list files under s specified directory, but this isnt what I need.

 

I need to know if the following is possible and some idea on how to go about it.

 

This will be the specified directory:

 

templates

 

Now I need the page to show all the subfolders and all the files in the subfolders listed.

 

eg.

 

COMMON - subfolder

header

footer

MEMBERS - subfolder

message

panel

 

etc

 

Any help is much appreciated :)

 

 

User a recursive function to list the files in the subdirectories..

function read_directory($dir_path){
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            if( is_dir($file) ){
                 read_directory($file);
            } else {
                echo "$file\n";
            }
        }
    }
    closedir($handle);
}
}

 

read_directory('/path/to/my/dir');

i didnt check the syntax but it should work.

I have got this, which I hoped would solve the problem, but it is only listing the subdirectories and not their files.

 

<?php 

function DisplayDir() { 

$MainDir=opendir("templates/"); 

while ($folder = readdir($MainDir)) { 

if ($folder == "." || $folder == "..") { } 
else {
print "<tr><td><font face=\"Verdana, Arial, Helvetica, sans-serif\">$folder</font> </td></tr></br>";

} 
} 
closedir($MainDir); 
return; 
} 

function DisplayFiles() { 

$SubDir=opendir($folder); 

while ($file = readdir($SubDir)) { 

if ($file == "." || $file == "..") { } 
else {
print "<tr><td><font face=\"Verdana, Arial, Helvetica, sans-serif\">$file</font> </td></tr></br>";

} 
} 
closedir($MainDir); 
return; 
} 


?> 
<b><font face="Verdana, Arial, Helvetica, sans-serif">Current Directory Contain 
Following files and Sub Directories...</font></b> 
<p> 
<?php 
@ DisplayDir(); 
@ DisplayFiles();
?>

 

Any help on solving this issue is much appreciated :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.