pcw Posted April 7, 2009 Share Posted April 7, 2009 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 Link to comment https://forums.phpfreaks.com/topic/153018-listing-subdirectories-and-their-contents/ Share on other sites More sharing options...
irvieto Posted April 7, 2009 Share Posted April 7, 2009 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. Link to comment https://forums.phpfreaks.com/topic/153018-listing-subdirectories-and-their-contents/#findComment-803703 Share on other sites More sharing options...
pcw Posted April 7, 2009 Author Share Posted April 7, 2009 The above does not work. $handle is not defined as anything. Also how do I make the function recursive? Thanks Link to comment https://forums.phpfreaks.com/topic/153018-listing-subdirectories-and-their-contents/#findComment-803852 Share on other sites More sharing options...
pcw Posted April 8, 2009 Author Share Posted April 8, 2009 Has anyone any idea how i can do this? Thanks Link to comment https://forums.phpfreaks.com/topic/153018-listing-subdirectories-and-their-contents/#findComment-804337 Share on other sites More sharing options...
pcw Posted April 8, 2009 Author Share Posted April 8, 2009 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 Link to comment https://forums.phpfreaks.com/topic/153018-listing-subdirectories-and-their-contents/#findComment-804487 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.