列出文件夹中的所有项目

时间:2011-10-29 17:16:31

标签: php html

我想写一个php脚本,它列出了文件夹中的所有项目,php脚本所在的位置/在启动它的文件夹中(index.php除外)...

我遇到了2个问题

1. ATM it is only listing the items in the folder "foobar"<br/>
2. It is not creating a new `<li>` Tag for every new items<br/>

到目前为止,这是我的代码......

<?php

$folder = openDir("foobar");

while ($file = readDir($folder)) {

 if ($file != "." && $file != "..") {

  echo "<!DOCTYPE HTML>
<html>
<head>
<title>nothing</title>
<link href='http://fonts.googleapis.com/css?family=Raleway:100' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Vollkorn:700' rel='stylesheet' type='text/css'>
<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />
</head>
<body>

<div id=\"title\">
<h1>nothing</h1>
</div>

<div id=\"main\">
<ul>
    <li><a href=\"foobar/$file\">$file</a></li>
</ul>
</div>

</body>
</html>";
 }
}

closeDir($folder);
?>

2 个答案:

答案 0 :(得分:2)

<!DOCTYPE HTML>
<html>
<head>
    <title>nothing</title>
    <link href='http://fonts.googleapis.com/css?family=Raleway:100' rel='stylesheet' type='text/css' />
    <link href='http://fonts.googleapis.com/css?family=Vollkorn:700' rel='stylesheet' type='text/css' />
    <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
    <div id="title">
        <h1>nothing</h1>
    </div>

    <div id="main">
        <ul>
<?php
$folder = openDir(__DIR__);
while ($file = readDir($folder)) {
    if ($file != "." && $file != ".." && $file != "index.php" && $file != "style.css") {
        echo "<li><a href=\"foobar/$file\">$file</a></li>";
    }
}
?>
        </ul>
    </div>
</body>
</html>

答案 1 :(得分:0)

如果您尝试列出一个html文档中的所有文件,请将“if(){}”仅包含<li>个项目