0

这是我服务器中的文件结构:

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

等等,好吧,我想打印任何文件和任何子文件夹的列表,如下所示:

<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>

我的问题是我不知道如何在 php 代码中获取列表。有人可以帮助我吗?

谢谢

4

1 回答 1

1

您可能想查看 readdir 和 scandir:http://php.net/manual/en/function.readdir.phphttp://www.php.net/manual/en/function.scandir.php。那里也有一些很好的例子。也许这样的事情可能会起作用(你需要稍微调整一下 - 原来是为 Windows 写的,抱歉):

$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);
}                   
于 2012-06-28T11:08:02.250 回答