2012-02-07 52 views
1

我需要以CLEAN json格式列出目录中的所有图像。我需要它看起来像这样:在JSON中列出目录中的所有图像

URLLINK:"image of where the image is" 

这就是我需要的。

+0

这样做第一从目录中读取所有图像,这将返回nam e,并将其编码为json格式.. – 2012-02-07 04:58:17

+1

[你有什么尝试?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – 2012-02-07 05:03:26

回答

8
$files = array(); 

$dir = opendir('/tmp'); 
while ($file = readdir($dir)) { 
    if ($file == '.' || $file == '..') { 
     continue; 
    } 

    $files[] = $file; 
} 

header('Content-type: application/json'); 
echo json_encode($files); 

就像这样。 你可以把它转换成任何结构你想

11

一个行代码是:

echo json_encode(glob("*.{jpg,gif,png}", GLOB_BRACE)); 

参考文献:

+0

Coool,但我仍然会添加标题:) – 2014-02-21 15:15:22

+0

这是一颗宝石!你知道如何将递归添加到列表子文件夹吗? – atripes 2017-09-07 10:54:49

+0

简直太棒了!谢谢! – 2017-09-13 17:28:30

相关问题