Babiker
Babiker

Reputation: 18818

Why is scandir() returning false for directories?

I have a directory named client_images that has dir_1, dir_2 and dir_3 as subdirectories. When i run the following:

foreach(scandir("client_images") as $key => $value ){
    if(is_dir($value)){
        echo $value." is a dir.<br>";
    }
    else{
        echo $value." is not a dir.<br>";
    }
}

i get :

. is a dir.
.. is a dir.
dir_1 is not a dir.
dir_2 is not a dir.
dir_3 is not a dir.

Why is php returning false for directories?

Upvotes: 0

Views: 4799

Answers (3)

Wh1T3h4Ck5
Wh1T3h4Ck5

Reputation: 8509

$mydir = "client_images/";

foreach(scandir($mydir) as $key => $value ){
  if(is_dir($mydir . $value)){
    echo $value." is a dir.<br>";
    }
  else{
    echo $value." is not a dir.<br>";
    }
  }

Upvotes: 2

Shameer
Shameer

Reputation: 3066

You can scan directory using DirectoryIterators. Use the below code to to check if a file is a directory or not

$iterator = new DirectoryIterator('path');
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isDir()) {
        echo $fileinfo->getFilename() . "\n";
    }
} 

Upvotes: 1

geekosaur
geekosaur

Reputation: 61467

You are doing scandir("client_images"); the result is bare filenames without the client_images/ path, so it's looking for them in the current directory. Naturally, the only directories likely to be in common between .. and client_images/ are . and .., the entries present in all directories.

Upvotes: 2

Related Questions