【发布时间】:2011-04-16 15:36:28
【问题描述】:
我们使用不允许目录列表的网络服务器。
我希望允许列出一个特定目录。
如何制作一个包含该目录内容的简单 HTML 文件?
【问题讨论】:
-
你有什么语言?
我们使用不允许目录列表的网络服务器。
我希望允许列出一个特定目录。
如何制作一个包含该目录内容的简单 HTML 文件?
【问题讨论】:
纯 HTML 无法做到这一点。
但是,如果您可以访问 Apache 服务器上的 PHP(您将帖子标记为“apache”),则可以轻松完成 - se the PHP glob function。如果没有 - 你可以试试 Server Side Include - 这是一个 Apache 的东西,我不太了解它。
【讨论】:
您可以: 编写一个服务器端脚本页面,如 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
【讨论】:
.htaccess 已禁用。我是一个真正的新手,所以一个简单的工作示例将不胜感激。
您是否尝试通过 .htaccess 允许此目录使用它?
Options +Indexes
我将它用于我的一些目录,其中目录列表被我的提供者禁用
【讨论】:
.htaccess 已禁用。添加这样的.htaccess时,我得到Internal Server Error。
对我来说,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 以及错误日志。然后它将显示带有链接的输出。这是非常简单的代码,易于编辑。
【讨论】:
有足够的正当理由明确禁用 apache 或其他 Web 服务器中的自动目录索引。或者,例如,您可能只想在索引中包含某些文件类型。在这种情况下,您可能仍希望为特定文件夹静态生成 index.html 文件。
treetree 是一个简约实用程序,可在大多数类 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 --help 或man tree,了解所有支持的选项。
我需要一个索引生成器,我可以按照我想要的方式设置样式,并且还包括文件大小,所以最后写了this script(python 3),它除了具有可自定义的样式外,还可以递归生成@所有嵌套子目录中的 987654339@ 文件(带有 --recursive 或 -r 标志)。样式大量借鉴了 caddyserver 的 file-server 模块。它包括上次修改时间,并且在移动视口中响应。
【讨论】:
index.html 文件中的目录,请将 --prune 添加到 tree 命令
如果您的登台服务器启用了目录列表,则可以将index.html 复制到生产服务器。
例如:
wget https://staging/dir/index.html
# do any additional processing on index.html
scp index.html prod/dir
【讨论】:
有一个由 Celeron Dude 制作的免费 php 脚本可以执行此操作,称为 Celeron Dude Indexer 2。它不需要 .htaccess 源代码易于理解并提供了一个很好的起点。
【讨论】:
如果你有node,那么你可以使用fs 像this 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
【讨论】: