Apache directory listing as json

风格不统一 提交于 2019-11-28 12:21:20

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.

I think I would do this instead...

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

DirectoryIndex ls_json.php index.php index.html

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);

You could use mod_dir as follows - create a php script and list your directories how you want (set content-type as appropriate).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!