这是我服务器中的文件结构:
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代码中获取列表。 有人可以帮帮我吗?
由于
答案 0 :(得分:1)
您可能希望查看readdir和scandir:http://php.net/manual/en/function.readdir.php和http://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);
}