代码之家  ›  专栏  ›  技术社区  ›  Nathan Osman

opendir/readdir有问题吗?

  •  2
  • Nathan Osman  · 技术社区  · 14 年前

    下面是我的PHP代码:

    <?php
    
    // Enumerate the directories in styles
    $styles_dir = 'styles/';
    
    if($handle = opendir($styles_dir))
    {
        while(FALSE !== ($file = readdir($handle)))
        {
            echo $file . '(' . is_dir($file) . ')<br>';
        }
    }
    ?>
    

    以下是目录 styles :

    http://files.quickmediasolutions.com/php.jpg

    下面是输出:

    .(1)
    ..(1)
    forest()
    industrial()
    

    为什么不 forest industrial 目录?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Mark Elliot    14 年前

    路径 is_dir 是相对于基文件的,因此您确实需要执行类似的测试

    is_dir($styles_dir . '/' . $file)
    

    注意,这是为 . .. “目录”,因为它们无处不在。

        2
  •  1
  •   codaddict    14 年前

    您需要将目录名作为文件名的前缀 is_dir 相对于当前目录工作。

    变化

    echo $file . '(' . is_dir($file) . ')<br>';
    

    echo $file . '(' . is_dir("$styles_dir/$file") . ')<br>';
    

    或者,您可以将目录更改为 $styles_dir 使用 chdir 然后您当前的代码就可以工作了。