roybatty
roybatty

Reputation: 191

print each subfolder and each files of subfolders php

This is the file structure in my server:

htdocs/pdf/a/sub-a/file-01.pdf
htdocs/pdf/a/sub-a/file-02.pdf
htdocs/pdf/a/sub-a/file-03.pdf

htdocs/pdf/a/sub-b/file-01.pdf
htdocs/pdf/a/sub-b/file-02.pdf
htdocs/pdf/a/sub-b/file-03.pdf

htdocs/pdf/a/sub-c/file-01.pdf
htdocs/pdf/a/sub-c/file-02.pdf
htdocs/pdf/a/sub-c/file-03.pdf

and so on, well, I would like to print a list of any file and any subfolder like this:

<h3>sub-a</h3>
<ul>
<li>file-01.pdf</li>
<li>file-02.pdf</li>
<li>file-03.pdf</li>
</ul>

<h3>sub-b</h3>
<ul>
<li>file-01.pdf</li>
<li>file-02.pdf</li>
<li>file-03.pdf</li>
</ul>

<h3>sub-c</h3>
<ul>
<li>file-01.pdf</li>
<li>file-02.pdf</li>
<li>file-03.pdf</li>
</ul>

my matter is i don't know how to get the list in php code. anybody can help me?

Thanks

Upvotes: 0

Views: 116

Answers (1)

Laurence Tuck
Laurence Tuck

Reputation: 420

You might want to look at readdir and scandir: http://php.net/manual/en/function.readdir.php and http://www.php.net/manual/en/function.scandir.php. some great examples there too. maybe something like this could work (you will need to tweek somewhat - original written for windows sorry):

$dir = "htdocs/pdf/a/";
if ($handle = opendir($dir)) {                  
  while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
          $dir2 = "htdocs/pdf/a/".$file."/";
          echo "<h3>".ucwords(str_replace("-"," ",$file))."</h3><ul>";  
          if ($handle2 = opendir($dir2)) {
              while (false !== ($file2 = readdir($handle2))) {
                if ($file2 != "." && $file2 != "..") {                                                                                                          
                  echo "<li>".$file2."</li>";

                }
              }
            closedir($handle2);
          }
          echo "</ul><hr size='1'>";
      }
  }                 
closedir($handle);
}                   

Upvotes: 1

Related Questions