如何编写PHP代码从目录扫描输出JSON?

时间:2014-03-20 04:17:14

标签: php json

需要扫描'图片'目录并列出所有文件夹名称,每个文件夹中的图像数量,文件夹中任何php文件的实际路径以及JSON格式的文件夹的上次修改日期。如果有人可以帮忙请。 JSON格式如下

[
    {
      "id":"1",
      "name":"Folder1",
      "images":"15",
      "url": "http://website.com/Picturs/file.php", 
      "uploaddate": "08/20/2011" 
    },
    {
      "id":"2",
      "name":"Folder2",
      "images":"25",
      "url": "http://website.com/Picturs/file.php", 
      "uploaddate": "08/31/2011" 
    },
    {
      "id":"3",
      "name":"Folder3",
      "images":"13",
      "url": "http://website.com/Picturs/file.php", 
      "uploaddate": "09/20/2011" 
    }
]

我想要做的是获取图片目录中的目录列表,每个目录将有一个php文件,该文件将包含该特定目录中所有图像的真实路径。 试图为iOS完成照片应用程序。我从这个文件列出专辑,每个文件夹中的php将用于向UICollectionView提供数据。

到目前为止,这是我的代码:

<?php
$directoryToScan = "*";

$json_array = array();

// Open a known directory, and proceed to read its contents
foreach(glob($directoryToScan, GLOB_ONLYDIR) as $folders) 
{

    //get total number of jpg files in each folder
    $num_files = count(glob("$folders/*.jpg"));


    //find a php file in each folder and get its realpath
    foreach (glob("$folders/*.json") as $filename) {

        //echo "$filename size " . filesize($filename) . "\n";
        $phpfile = realpath($filename);
        //echo $phpfile;
    }

    //get date on which each folder was created.
    $fileDate = date("mdY", filectime($folders));   

    $json_Array[] = array('name'=>$folders,'images'=>$num_files,'url'=>$phpfile,'uploaddate'=>$fileDate);

}

echo(json_encode($json_Array));

?>

需要获得&#34; id&#34;这是自动递增的,并且url的格式也不正确。 目前显示如下

"http:\/\/www.website.com\/Pictures\/image_001.jpg"

"http://www.website.com/Pictures/image_001.jpg"

的实例

2 个答案:

答案 0 :(得分:2)

搞定了。感谢Stackoverflow

<?php
$directoryToScan = "*";

define('WEBSITE', "http://www.website.com/pictures/");

$json_array = array();

// Open a known directory, and proceed to read its contents
foreach(glob($directoryToScan, GLOB_ONLYDIR) as $folders) 
{
    //get total number of jpg files in each folder
    $num_files = count(glob("$folders/*.jpg"));
    $totalFiles = (string)$num_files;

    //find a php file in each folder and get its realpath
    foreach (glob("$folders/*.json") as $filename) {

        $turl = WEBSITE.$filename;
        $url = str_replace("\/", "\\", $turl);
        //echo($url);
    }

    //get date on which each folder was created.
    $fileDate = date("mdY", filectime($folders));   

    $json_Array[] = array('name'=>$folders,'images'=>$num_files,'url'=>$url,'uploaddate'=>$fileDate);

}

echo(json_encode($json_Array));

?>

发布代码以防万一有人帮助。

答案 1 :(得分:0)

帮我修改Hitz的代码

<?php
$directoryToScan = "images/portfolio";

define('WEBSITE', "http://site.com.br/");

$json_array = array();

// Open a known directory, and proceed to read its contents
foreach(glob($directoryToScan, GLOB_ONLYDIR) as $folders) 
{
    //get total number of jpg files in each folder
    $num_files = count(glob("$folders/*.jpg"));
    $totalFiles = (string)$num_files;
    //echo $totalFiles;

    foreach (glob("$folders/*.jpg") as $filename) {

        $turl = WEBSITE.$filename;
        $url = str_replace("\/", "\\", $turl);
        //echo($url);

        //get date on which each folder was created.
        $fileDate = date("mdY", filectime($folders));   

        $json_Array[$filename] = array('name'=>$folders,'images'=>$num_files,'url'=>$url,'uploaddate'=>$fileDate);
    }


}

echo(json_encode($json_Array));

?>

输出:

{
    "images/portfolio/01-grande.jpg": {
        "name": "images/portfolio",
        "images": 16,
        "url": "http:/site.com.br/images/portfolio/01-grande.jpg",
        "uploaddate": "03222019"
    },
    "images/portfolio/01-pequena.jpg": {
        "name": "images/portfolio",
        "images": 16,
        "url": "http:/site.com.br/images/portfolio/01-pequena.jpg",
        "uploaddate": "03222019"
    },
    "images/portfolio/02-grande.jpg": {
        "name": "images/portfolio",
        "images": 16,
        "url": "http:/site.com.br/images/portfolio/02-grande.jpg",
        "uploaddate": "03222019"
    }
}