如何编写PHP代码以从目录扫描输出JSON? [英] How to write PHP code to output JSON from directory scan?

查看:183
本文介绍了如何编写PHP代码以从目录扫描输出JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Need to scan 'Pictures' directory and list all the folder names, number of images in each folder, realpath of any php file in the folder and last modified date of the folder in JSON format. If anyone can help please. JSON format is as under

[
    {
      "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提供数据.

What I'm trying to do is get list of directories in Pictures Directory and each directory will have one php file which will have realpath of all the images in that perticular directory. Trying to get a photo app done for iOS. Where I list albums from this file and the php in each folder will be used to provide data to the UICollectionView.

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

This is my code so far :

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

?>

需要获取"id",该ID会自动递增,并且url的格式也不正确. 当前它显示为

Need to get the "id" which is auto incremented and also the url is not in correct format. currently it displays like this

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

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

推荐答案

使其正常运行.多亏了Stackoverflow

Got it working. Thanks to 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));

?>

发布代码以防万一.

这篇关于如何编写PHP代码以从目录扫描输出JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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