代码之家  ›  专栏  ›  技术社区  ›  reefnet_alex

php is_dir对于不存在的文件夹返回true

php
  •  1
  • reefnet_alex  · 技术社区  · 17 年前

    有人遇到过这种怪事吗?

    我正在检查我的一个单元测试中是否存在多个目录。 is_dir 正在报告为真(1),尽管调用时文件夹不存在。代码如下(带有一些额外的中间变量以便于调试):

    foreach($userfolders as $uf) {
        $uf = sprintf($uf, $user_id);
        $uf = ltrim($uf,'/');
        $path = trim($base . '/' . $uf);
    
        $res = is_dir($path); //returns false except last time returns 1
        $this->assertFalse($res, $path);
    }
    

    运行带有php版本5.2.4-2ubuntu 5.3的UbuntuLinux8.04的机器

    我检查过的事情:

     - Paths are full paths
     - The same thing happens on two separate machines (both running Ubuntu)
     - I have stepped through line by line in a debugger
     - Paths genuinely don't exist at the point where is_dir is called
     - While the code is paused on this line, I can actually drop to a shell and run 
    

    并得到正确的结果 -路径都在256个字符以下 -我无法想象权限问题,因为文件夹不存在!父文件夹不能导致权限问题,因为循环中的其他文件夹已正确报告为丢失。

    对php文档的评论指出了 伊斯迪尔 但不是这个特别的。

    我不是把这个贴成“请帮助我修复”,而是希望有人遇到同样的事情,可以在这里搜索和 有希望地 看到这个的人的回答!

    3 回复  |  直到 7 年前
        1
  •  3
  •   Thomas Owens    17 年前

    我不认为这会引起你的问题,但是$path有尾随斜杠,对吗?

        2
  •  1
  •   reefnet_alex    17 年前

    为了它的价值, is_readable 可以用作工作区。

        3
  •  0
  •   dragonmantank    17 年前
    $path = trim($base . '/' . $uf);
    

    这可能就是原因。我假设$base是您正在搜索的某个根文件夹,所以如果$uf是类似于“”、'.'或'../'的文件,它可能返回true。我们必须看看您在前臂中使用了什么值,才能进一步了解更多信息。

    [编辑]

    在OpenBSD 4.3和PHP5.2上再看一下上面的代码就可以了。

    推荐文章