繁体   English   中英

PHP readdir无法读取某些文件

[英]PHP readdir is not reading some files

我正在使用以下代码遍历目录以打印出文件名。 但是,并非所有文件都显示。 我尝试使用clearstatcache无效。

    $str = '';
    $ignore = array('.', '..');

    $dh = @opendir( $path );
    if ($dh === FALSE)
    {
        // error
    }

    $file = readdir( $dh );
    while( $file !== FALSE )
    {
        if (in_array($file, $ignore, TRUE)) { break; }
        $str .= $file."\n";
        $file = readdir( $dh );
    }

现在是目录的内容:

root.auth  test1.auth  test2.auth  test3.auth  test5.auth

但是,不会出现test5.auth。 如果我将其重命名为test4.auth,则不会出现。 如果我将其重命名为test6.auth,它的确会出现。 这是可靠的行为-我可以将其重命名几次,除非将其重命名为test6.auth,否则它仍然不会显示。

到底会发生什么?

我正在运行带有PHP版本5.2.6的Arch Linux(内核2.6.26-ARCH)和带有Suhosin-Patch的Apache / 2.2.9。 我的文件系统是ext3,运行的是fam 2.6.10。

继续也不会起作用,因为您将跳过读取下一个文件的行。

您可以摆脱第一个$file = readdir( $dh ); 然后做

while (false !== ($file = readdir($dh))) {
    if (in_array($file, $ignore, TRUE)) { continue; }
    $str .= $file."\n";
}

您的break关键字弄乱了您的代码:
您的循环很可能首先遇到“。”。 目录,然后退出while循环。

尝试用continue替换它,应该没问题。

if (in_array($file, $ignore, TRUE)) { break; }

当然,应该是continuebreak

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM