4

我有这个功能:

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;

                }
        }
}

这将返回:

directory1 , directory2, directory3 etc.. etc..

但是,如果我这样做:

    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;
                    }

                }
        }
}

它什么也不打印!

为什么会发生这种情况?我正在使用 windows 和 XAMPP 运行脚本以进行测试。该目录实际上包含目录。

谢谢

4

3 回答 3

11

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

于 2010-04-19T10:14:48.527 回答
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

于 2010-04-19T10:15:26.023 回答
1

因为$dir是完整路径,而 as$subdir只是路径片段

于 2010-04-19T10:16:38.227 回答