如何创建一个列出所有文件/目录的简单 index.html 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3785055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create a simple index.html file which lists all files/directories?
提问by David B
We use a web server that does not allow directory listing.
我们使用不允许目录列表的网络服务器。
There is a specific directory I would like to allow listing of.
我想允许列出一个特定的目录。
How can make a simple HTML file that will contain the contents of this directory?
如何制作一个包含此目录内容的简单 HTML 文件?
采纳答案by DuduAlul
You can either: Write a server-side script page like PHP, JSP, ASP.net etc to generate this HTML dynamically
您可以: 编写服务器端脚本页面(如 PHP、JSP、ASP.net 等)来动态生成此 HTML
or
或者
Setup the web-server that you are using (e.g. Apache) to do exactly that automatically for directories that doesn't contain welcome-page (e.g. index.html)
设置您正在使用的网络服务器(例如 Apache)为不包含欢迎页面(例如 index.html)的目录自动执行此操作
Specifically in apache read more here: Edit the httpd.conf: http://justlinux.com/forum/showthread.php?s=&postid=502789#post502789
具体在 apache 中阅读更多内容:编辑 httpd.conf:http: //justlinux.com/forum/showthread.php?s=&postid= 502789#post502789
or add the autoindex mod: http://httpd.apache.org/docs/current/mod/mod_autoindex.html
或添加自动索引模块:http: //httpd.apache.org/docs/current/mod/mod_autoindex.html
回答by ccpizza
There are enough valid reasons to explicitly disable automatic directory indexes in apache or other web servers. Or, for example, you might want to only include certain file types in the index. In such cases you might still want to have a statically generated index.htmlfile for specific folders.
有足够的正当理由在 apache 或其他 Web 服务器中明确禁用自动目录索引。或者,例如,您可能只想在索引中包含某些文件类型。在这种情况下,您可能仍希望为特定文件夹静态生成index.html文件。
This can be easily achieved with tree- a minimalistic utility that is available on most unix-like systems (ubuntu/debian: sudo apt install tree
, mac: brew install tree
) and which can generate plain text, XML, JSON or HTML output.
这可以通过tree轻松实现- 一种简约实用程序,可在大多数类 Unix 系统(ubuntu/debian: sudo apt install tree
, mac: brew install tree
)上使用,并且可以生成纯文本、XML、JSON 或 HTML 输出。
Generate an HTML directory index one level deep:
生成一层深的 HTML 目录索引:
tree -H '.' -L 1 --noreport --charset utf-8 > index.html
Only include specific file types that match a glob pattern, e.g. *.zip
files:
仅包含匹配 glob 模式的特定文件类型,例如*.zip
文件:
tree -H '.' -L 1 --noreport --charset utf-8 -P "*.zip" > index.html
The argument to
-H
is what will be used as a base href, so you can pass either a relative path such as.
or an absolute path from the web root, such as/files
.-L 1
limits the listing to the current directory only.
到的参数
-H
是什么将被用作基本href,这样可以通过其中一个相对路径诸如.
从web根或绝对路径,如/files
。-L 1
将列表限制为仅当前目录。
I wanted an index generator which I could style the way I wanted, so ended up using this script— in addition to having customizable styling, the script will also recursively generate the index.html
file in all the nested subdirectories.
我想要一个可以按照我想要的方式设置样式的索引生成器,所以最终使用了这个脚本——除了具有可自定义的样式外,该脚本还将index.html
在所有嵌套子目录中递归生成文件。
回答by ryryan
For me PHP is the easiest way to do it:
对我来说,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);
?>
Place this in your directory and set where you want it to search on the $path. The first if statement will hide your php file and .htaccess and the error log. It will then display the output with a link. This is very simple code and easy to edit.
将它放在您的目录中并设置您希望它在 $path 上搜索的位置。第一个 if 语句将隐藏您的 php 文件和 .htaccess 以及错误日志。然后它将显示带有链接的输出。这是非常简单的代码,易于编辑。
回答by Michael
Did you try to allow it for this directory via .htaccess?
您是否尝试通过 .htaccess 允许它访问此目录?
Options +Indexes
I use this for some of my directories where directory listing is disabled by my provider
我将此用于我的某些目录,其中我的提供商禁用了目录列表
回答by Michael Banzon
This can't be done with pure HTML.
这不能用纯 HTML 来完成。
However if you have access to PHP on the Apache server (you tagged the post "apache") it can be done easilly - se the PHP glob function. If not - you might try Server Side Include - it's an Apache thing, and I don't know much about it.
但是,如果您可以访问 Apache 服务器上的 PHP(您将帖子标记为“apache”),则可以轻松完成 -使用 PHP glob 函数。如果没有 - 您可以尝试服务器端包含 - 它是 Apache 的东西,我对此知之甚少。
回答by wisbucky
If you have a staging server that has directory listing enabled, then you can copy the index.html
to the production server.
如果您的登台服务器启用了目录列表,则可以将其复制index.html
到生产服务器。
For example:
例如:
wget https://staging/dir/index.html
# do any additional processing on index.html
scp index.html prod/dir