2013-12-08 34 views
4

是否有可能在Apache的目录列表返回JSON而不是HTML?Apache目录列表为json

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

+0

你看着[mod_dir(HTTP://httpd.apache。组织/文档/ 2.2/MOD/mod_dir.html)? –

+0

好的结果 - 这正是我需要使用的。 – Rjoss

回答

4

我查看了modules/generators/mod_autoindex.c中apache源代码,并且HTML生成是静态的。你可能重写此输出JSON,只需搜索所有ap_rputsap_rvputs函数调用,并用适当的JSON替换HTML。虽然这似乎很多工作。

我想我会做到这一点,而不是...

在这个网站上,切换到Apache的配置...

DirectoryIndex ls_json.php index.php index.html 

,然后将ls_json.php脚本到任意目录中,而您希望有一个JSON编码的房源:

// 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); 
+0

这几乎是我最终使用的。太棒了。 – Rjoss

1

你可以按如下方式使用mod_dir - 创建一个php脚本并列出你想要的目录(根据需要设置内容类型)。