【问题标题】:How can I create a simple index.html file which lists all files/directories?如何创建一个列出所有文件/目录的简单 index.html 文件?
【发布时间】:2011-04-16 15:36:28
【问题描述】:

我们使用不允许目录列表的网络服务器。

我希望允许列出一个特定目录。

如何制作一个包含该目录内容的简单 HTML 文件?

【问题讨论】:

  • 你有什么语言?

标签: html webserver


【解决方案1】:

纯 HTML 无法做到这一点。

但是,如果您可以访问 Apache 服务器上的 PHP(您将帖子标记为“apache”),则可以轻松完成 - se the PHP glob function。如果没有 - 你可以试试 Server Side Include - 这是一个 Apache 的东西,我不太了解它。

【讨论】:

  • 我从来没有写过 PHP。如果你能给出一个我可以放在我的目录中的 HTML 的工作示例 - 那就太好了!
【解决方案2】:

您可以: 编写一个服务器端脚本页面,如 PHP、JSP、ASP.net 等以动态生成此 HTML

设置您正在使用的网络服务器(例如 Apache)为不包含欢迎页面的目录(例如 index.html)自动执行此操作

特别是在 apache 中阅读更多内容: 编辑 httpd.conf: http://justlinux.com/forum/showthread.php?s=&postid=502789#post502789(更新链接:https://forums.justlinux.com/showthread.php?94230-Make-apache-list-directory-contents&highlight=502789

或添加自动索引模块: http://httpd.apache.org/docs/current/mod/mod_autoindex.html

【讨论】:

  • apache 服务器不受我控制。 .htaccess 已禁用。我是一个真正的新手,所以一个简单的工作示例将不胜感激。
  • 你的 apache 支持 PHP 吗?您必须使用支持服务器端脚本编写的 apache,否则这是不可能的..
  • 添加一些像hello.php这样的php文件,编辑这个文件:" ",尝试从客户端server/hello.php访问它,看看是什么你得到..
  • @MrOhad 这行得通。那么我应该在 HTML 中噘嘴以允许列表吗?
【解决方案3】:

您是否尝试通过 .htaccess 允许此目录使用它?

Options +Indexes

我将它用于我的一些目录,其中目录列表被我的提供者禁用

【讨论】:

  • 那是我的第一选择,但 apache 服务器不受我控制,而且似乎 .htaccess 已禁用。添加这样的.htaccess时,我得到Internal Server Error
【解决方案4】:

对我来说,PHP 是最简单的方法:

<?php
echo "Here are our files";
$path = ".";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh)) !== false) {
    if($file != "." && $file != ".." && $file != "index.php" && $file != ".htaccess" && $file != "error_log" && $file != "cgi-bin") {
        echo "<a href='$path/$file'>$file</a><br /><br />";
        $i++;
    }
}
closedir($dh);
?> 

将它放在你的目录中,并设置你希望它在 $path 上搜索的位置。第一个 if 语句将隐藏您的 php 文件和 .htaccess 以及错误日志。然后它将显示带有链接的输出。这是非常简单的代码,易于编辑。

【讨论】:

  • 这有多个存储的XSS漏洞:文件名可以包含引号和等
  • 当我在 AWS 上尝试此操作时,$file
    ;$i++;}}closedir($dh);?> 是具有指定间距的结果
【解决方案5】:

有足够的正当理由明确禁用 apache 或其他 Web 服务器中的自动目录索引。或者,例如,您可能只想在索引中包含某些文件类型。在这种情况下,您可能仍希望为特定文件夹静态生成 index.html 文件。

tree

tree 是一个简约实用程序,可在大多数类 unix 系统上使用(ubuntu/debian:sudo apt install tree,mac:brew install tree,windows:zip)。 tree 可以生成纯文本、XML、JSON 或 HTML 输出。

生成一个深度一级的 HTML 目录索引:

tree -H '.' -L 1 --noreport --charset utf-8 -o index.html

仅包含与 glob 模式匹配的特定文件类型,例如*.zip文件:

tree -H '.' -L 1 --noreport --charset utf-8 -P "*.zip" -o index.html

-H 的参数将用作基本 href,因此您可以传递相对路径(例如 .)或来自 Web 根目录的绝对路径(例如 /files)。 -L 1 将列表限制为仅当前目录。

在您的终端中查看tree --helpman tree,了解所有支持的选项。

具有递归遍历的生成器脚本

我需要一个索引生成器,我可以按照我想要的方式设置样式,并且还包括文件大小,所以最后写了this script(python 3),它除了具有可自定义的样式外,还可以递归生成@所有嵌套子目录中的 987654339@ 文件(带有 --recursive-r 标志)。样式大量借鉴了 caddyserver 的 file-server 模块。它包括上次修改时间,并且在移动视口中响应。

【讨论】:

  • 优秀的解决方案!!!如果您想省略 index.html 文件中的目录,请将 --prune 添加到 tree 命令
  • 受你的剧本启发,我写了https://github.com/yencarnacion/html4tree。 html4tree 是用 Kotlin 编写的。
  • @ccpizza,该脚本的链接已损坏。
  • 这是一个很好的解决方案,可以将站点部署到像 GitLab Pages 这样只允许静态 Web 内容(即没有 php)并且不允许使用 .htaccess 的地方。
  • 感谢您的脚本!当我需要快速破解时,它工作得很好。万岁。
【解决方案6】:

如果您的登台服务器启用了目录列表,则可以将index.html 复制到生产服务器。

例如:

wget https://staging/dir/index.html

# do any additional processing on index.html

scp index.html prod/dir

【讨论】:

    【解决方案7】:

    有一个由 Celeron Dude 制作的免费 php 脚本可以执行此操作,称为 Celeron Dude Indexer 2。它不需要 .htaccess 源代码易于理解并提供了一个很好的起点。

    这是一个下载链接:https://gitlab.com/desbest/celeron-dude-indexer/

    【讨论】:

      【解决方案8】:

      如果你有node,那么你可以使用fsthis answer 来获取所有文件:

      const { resolve } = require('path'),
        { readdir } = require('fs').promises;
      
      async function getFiles(dir) {
        const dirents = await readdir(dir, { withFileTypes: true });
        const files = await Promise.all(dirents.map((dirent) => {
          const res = resolve(dir, dirent.name);
          return dirent.isDirectory() ? getFiles(res) : res;
        }));
        return Array.prototype.concat(...files);
      }
      

      你可以这样使用:

      const directory = "./Documents/";
        
      getFiles(directory).then(results => {
        const html = `<ul>` +
        results.map(fileOrDirectory => `<li>${fileOrDirectory}</li>`).join('\n') +
        `</ul>`;
      
        process.stdout.write(html);
        // or you could use something like fs.writeFile to write the file directly
      });
      

      您可以在命令行中调用它,如下所示:

      $ node thatScript.js > index.html
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-13
        • 2012-09-02
        • 1970-01-01
        • 2013-06-21
        • 1970-01-01
        相关资源
        最近更新 更多