is_dir 无法识别目录。为什么?

发布于 2024-08-29 21:39:05 字数 945 浏览 2 评论 0原文

我有这个函数:

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 运行脚本以进行测试。该目录实际上包含目录。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

手长情犹 2024-09-05 21:39:05

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

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

旧话新听 2024-09-05 21:39:05

readdir() 只给出文件/目录名称,而不给出完整路径(显然 is_dir 需要)。

在这里找到 - http://www.php.net/manual /en/function.is-dir.php#79622

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

中性美 2024-09-05 21:39:05

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文