Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is_dir doesn't recognize directories. Why?

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

like image 877
0plus1 Avatar asked Dec 06 '22 03:12

0plus1


2 Answers

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

like image 53
binaryLV Avatar answered Dec 18 '22 09:12

binaryLV


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

like image 40
Mathew Avatar answered Dec 18 '22 07:12

Mathew