列出文件php from - 创建json文件

时间:2016-12-05 12:19:16

标签: php json

我想创建一个php脚本,从3目录加载我的所有文件(.pdf)并创建一个json文件。

我试试这个

$dir_2016 = "./HCL/2016";
$dir_2015 = "./HCL/2015";
$dir_2014 = "./HCL/2014";

$files_2016 = array();
$files_2015 = array();
$files_2014 = array();

$json_file = array(
    "2016" => $files_2016,
    "2015" => $files_2015,
    "2014" => $files_2014
);  

if(is_dir($dir_2016) and is_dir($dir_2015) and is_dir($dir_2014))
{
    // 2016
    if(is_dir($dir_2016))
    {
        if($dh = opendir($dir_2016))
        {
            while(($file = readdir($dh)) != false)
            {
                if($file == "." or $file == ".."){

                } else {
                    $files_2016[] = $file; // Add the file to the array
                }
            }
        }
    }

    // 2015
    if(is_dir($dir_2015))
    {
        if($dh = opendir($dir_2015))
        {
            while(($file = readdir($dh)) != false)
            {
                if($file == "." or $file == ".."){

                } else {
                    $files_2015[] = $file; // Add the file to the array
                }
            }
        }
    }

    // 2014
    if(is_dir($dir_2014))
    {
        if($dh = opendir($dir_2014))
        {
            while(($file = readdir($dh)) != false)
            {
                if($file == "." or $file == ".."){

                } else {
                    $files_2014[] = $file; // Add the file to the array
                }
            }
        }
    }       
    echo json_encode($json_file);
}

但输出是:

{"2016":[],"2015":[],"2014":[]}

files_2014 [],files_2015 [],files_2016 []为空。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

根据我上面的评论,这里有一个便宜的方式来获取给定目录中的pdf文件名:

mymap = L.map('map_container').setView([24.379311, 88.041001], 15);
mymap2 = L.map('map_container2').setView([24.379311, 88.041001], 15);

L.tileLayer(
     '', {
        maxZoom: 18,
        minZoom:1,
}).addTo(mymap);

L.tileLayer(
     '', {
        maxZoom: 18,
        minZoom:1,
}).addTo(mymap2);

shpfile = new L.Shapefile('js/world/TM_WORLD_BORDERS-0.3', {
    style: stateStyle
});
shpfile.addTo(mymap);
shpfile.once("data:loaded", function() {
    console.log("finished loaded shapefile");
});

shpfile2 = new L.Shapefile('js/world/TM_WORLD_BORDERS-0.3', {
    style: stateStyle
});
shpfile2.addTo(mymap2);
shpfile2.once("data:loaded", function() {
    console.log("finished loaded shapefile2");
});

请注意,还有很多其他方法可以获取目录中的所有文件,因此这绝不是唯一的解决方案。