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

I am using ajax to php to return how many folder I had inside that src path , I can count the folder number on ajax , but something wrong with my php file , it seem wont check how many folder I have.

My intention is to use ajax and php check how many folder i have and push those name into the array $files. Can anyone help me take a look. I have no experience one this.

Soroush
  • 907
  • 1
  • 9
  • 27
Hacktor
  • 83
  • 2
  • 10
  • What is the output of $files? Is the search path correct? Any errors or warnings? – Jite Oct 23 '15 at 06:44
  • 1
    You can't just echo an array - take a look here: http://stackoverflow.com/a/9816958/5422174 – Fanax Oct 23 '15 at 06:47

1 Answers1

0

If you only want to return the number of directories in the given path, you can easily use count and glob, see below

// 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;
Alex Andrei
  • 7,315
  • 3
  • 28
  • 42