Apache 目录列表为 json [英] Apache directory listing as json

查看:17
本文介绍了Apache 目录列表为 json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以让 apache 中的目录列表返回 json 而不是 html?

Is it possible to have the directory listing in apache return json instead of html?

我对 Apache 完全没有经验,但我浏览了 IndexOptions 和 mod_autoindex 的文档.似乎没有内置的方式来配置输出.

I'm completely unexperienced with Apache, but I've browsed the documentation for IndexOptions and mod_autoindex. It seems like there's no built in way to configure the output.

推荐答案

我查看了 modules/generators/mod_autoindex.c 中 apache 源代码中的代码,HTML 生成是静态的.您可以将其重写为输出 JSON,只需搜索所有 ap_rputsap_rvputs 函数调用并将 HTML 替换为适当的 JSON.不过,这似乎需要做很多工作.

I looked at the code in apache source in modules/generators/mod_autoindex.c and the HTML generation is static. You could rewrite this to output JSON, simply search for all the ap_rputs and ap_rvputs function calls and replace the HTML with the appropriate JSON. That's seems like a lot of work though.

我想我会这样做...

在此站点的 Apache 配置中,更改为...

In the Apache configuration for this site, change to...

DirectoryIndex ls_json.php index.php index.html

然后将 ls_json.php 脚本放入您想要 JSON 编码列表的任何目录中:

And then place ls_json.php script into the any directory for which you want a JSON encoded listing:

// grab the files
$files = scandir(dirname(__FILE__));

// remove "." and ".." (and anything else you might not want)
$output = [];
foreach ($files as $file)
  if (!in_array($file, [".", ".."]))
    $output[] = $file;

// out we go
header("Content-type: application/json");
echo json_encode($output);

这篇关于Apache 目录列表为 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆