4

I have this function:

if (is_dir($dir)) {
        //are we able to open it?
        if ($dh = opendir($dir)) {
            //Let's cycle
            while (($subdir = readdir($dh)) !== false) {
                if ($subdir != "." && $subdir != "..") {

                    echo $subdir;

                }
        }
}

This returns:

directory1 , directory2, directory3 etc.. etc..

Hoever if I do this:

    if (is_dir($dir)) {
        //are we able to open it?
        if ($dh = opendir($dir)) {
            //Let's cycle
            while (($subdir = readdir($dh)) !== false) {
                if ($subdir != "." && $subdir != "..") {

                    if (is_dir($subdir)) { 
                       echo $subdir;
                    }

                }
        }
}

It doesn't print nothing!

Why does this happens? I'm running the script withing windows and XAMPP for testing purposes. The directory does in fact contain directories.

Thank you

0plus1
  • 4,475
  • 12
  • 47
  • 89
  • 1
    *(related)* http://stackoverflow.com/questions/2418068/php-spl-recursivedirectoryiterator-recursiveiteratoriterator-retrieving-the-full/2655620#2655620 – Gordon Apr 19 '10 at 10:13

3 Answers3

11

is_dir($dir . '/' . $subdir)

binaryLV
  • 9,002
  • 2
  • 40
  • 42
4

readdir() only gives the file/dir name and not the full path (which is_dir apparently needs).

Found here - http://www.php.net/manual/en/function.is-dir.php#79622

Mathew
  • 8,203
  • 6
  • 37
  • 59
1

Its because $dir is a full path where as $subdir is only a path fragment

robjmills
  • 18,438
  • 15
  • 77
  • 121