ajax php检查我在路径src中有多少个文件夹

时间:2015-10-23 06:41:12

标签: php ajax

<?php
header("content-type: application/json");
$files = array();
$dir = "Img/House"; //folder src path

$dirHandle = opendir($dir); 

while(($file = readdir($dirHandle) !== false)){
    if ($file !== "." && $file !== "..")
    {
        $files[] = $file;
    }
}

echo($files);
//echo json_encode($directoryfiles);
?>

我使用ajax到php来返回我在src路径中有多少文件夹,我可以计算ajax上的文件夹编号,但我的php文件有问题,似乎不会检查我有多少文件夹。

我的意图是使用ajax和php检查我有多少文件夹并将这些名称推送到数组$files。任何人都可以帮我看看。我没有这方面的经验。

1 个答案:

答案 0 :(得分:0)

如果您只想返回给定路径中的目录数,可以轻松使用countglob,请参阅下文

// this is not needed unless you output json
// header("content-type: application/json");

$dir = "Img/House"; //folder src path

$dirs = glob($dir . "/*",GLOB_ONLYDIR);

print count($dirs);

// or directly
// print count(glob($dir . "/*",GLOB_ONLYDIR));

// if glob returns the current and parent dirs, "." and ".."
// just remove 2 from the count
// test by doing
print_r($dirs);
// then
print $count($dirs)-2;