为什么 opendir 不显示名称为单个整数的文件夹

发布于 2024-10-14 09:47:16 字数 1904 浏览 4 评论 0原文

我有一个脚本,可以打开一个目录,检查文件夹是否与数组匹配,然后打开它们。 该目录中有一个类似“apache2-50”的文件夹列表,但是当脚本打开该文件夹时,它只显示 .DS_Store 文件。这是输出:

This-is-not-a-MacBook:backend code archangel$ php -f frolic.php "/Users/archangel/Desktop/Httpbench Files/results"
Test Found apache2 in directory /Users/archangel/Desktop/Httpbench Files/results/apache2-50
--/Users/archangel/Desktop/Httpbench Files/results/apache2-50/.DS_Store

但这里是目录列表:

This-is-not-a-MacBook:apache2-50 archangel$ ls
0   1   2

现在我试图找出为什么我的 php 脚本没有显示这些文件夹。当我将文件夹“0”更改为“3”时,它可以工作:

This-is-not-a-MacBook:apache2-50 archangel$ ls
1   2   3

This-is-not-a-MacBook:backend code archangel$ php -f frolic.php "/Users/archangel/Desktop/Httpbench Files/results"
Test Found apache2 in directory /Users/archangel/Desktop/Httpbench Files/results/apache2-50
--/Users/archangel/Desktop/Httpbench Files/results/apache2-50/.DS_Store
--/Users/archangel/Desktop/Httpbench Files/results/apache2-50/1
--/Users/archangel/Desktop/Httpbench Files/results/apache2-50/2
--/Users/archangel/Desktop/Httpbench Files/results/apache2-50/3

这是我正在运行的代码:

#!/bin/php

//...

$dir = opendir($argv[1]);
//Opened the directory;

while($file = readdir($dir)){
//Loops through all the files/directories in our directory;
    if($file!="." && $file != ".."){
        $f = explode("-", $file);
        if(in_array($f[0], $servers) and in_array($f[1], $tests)) {
            echo "Test Found $f[0] in directory $argv[1]/$f[0]-$f[1]\n";
            $sdir = opendir("$argv[1]/$f[0]-$f[1]");
            while($sfile = readdir($sdir)){
                if($sfile!="." && $sfile != ".."){
                    echo "--$argv[1]/$f[0]-$f[1]/$sfile\n";
                }
            }
        }
    }
}

这可能是我的脚本有问题,还是 php(PHP 5.3.3) 中的错误? 谢谢

I have a script that opens a directory, checks if the folders matches an array, and then opens them.
In the directory there is a list of folders like "apache2-50", but when the script opens that folder, it only displays the .DS_Store file. Here is the output:

This-is-not-a-MacBook:backend code archangel$ php -f frolic.php "/Users/archangel/Desktop/Httpbench Files/results"
Test Found apache2 in directory /Users/archangel/Desktop/Httpbench Files/results/apache2-50
--/Users/archangel/Desktop/Httpbench Files/results/apache2-50/.DS_Store

But here is the directory listing:

This-is-not-a-MacBook:apache2-50 archangel$ ls
0   1   2

Now what I am trying to figure out why my php script is not showing those folders. When I change the folder "0" to "3" it works:

This-is-not-a-MacBook:apache2-50 archangel$ ls
1   2   3

This-is-not-a-MacBook:backend code archangel$ php -f frolic.php "/Users/archangel/Desktop/Httpbench Files/results"
Test Found apache2 in directory /Users/archangel/Desktop/Httpbench Files/results/apache2-50
--/Users/archangel/Desktop/Httpbench Files/results/apache2-50/.DS_Store
--/Users/archangel/Desktop/Httpbench Files/results/apache2-50/1
--/Users/archangel/Desktop/Httpbench Files/results/apache2-50/2
--/Users/archangel/Desktop/Httpbench Files/results/apache2-50/3

Here is the code that I am running:

#!/bin/php

//...

$dir = opendir($argv[1]);
//Opened the directory;

while($file = readdir($dir)){
//Loops through all the files/directories in our directory;
    if($file!="." && $file != ".."){
        $f = explode("-", $file);
        if(in_array($f[0], $servers) and in_array($f[1], $tests)) {
            echo "Test Found $f[0] in directory $argv[1]/$f[0]-$f[1]\n";
            $sdir = opendir("$argv[1]/$f[0]-$f[1]");
            while($sfile = readdir($sdir)){
                if($sfile!="." && $sfile != ".."){
                    echo "--$argv[1]/$f[0]-$f[1]/$sfile\n";
                }
            }
        }
    }
}

Could this be something wong with my script, or a bug in php(PHP 5.3.3)?
Thanks

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

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

发布评论

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

评论(3

最偏执的依靠 2024-10-21 09:47:16

这是 PHP 中字符串 "0" 计算结果为 false 的(非常讨厌的)副作用。当这种情况发生时,您的 while 循环

while($file = readdir($dir))

将会中断。

这应该可行,因为只有当 readdir() 实际返回 false 时它才会中断:(

while(($file = readdir($dir)) !== false)

显然,相应地更改两个循环,而不仅仅是外部循环。)

This is a (very nasty) side effect of the string "0" evaluating to false in PHP. When that happens, your while loop

while($file = readdir($dir))

will break.

This should work, because it breaks only when readdir() actually returns false:

while(($file = readdir($dir)) !== false)

(obviously, change both loops accordingly, not just the outer one.)

如梦亦如幻 2024-10-21 09:47:16

你为什么要使用opendir?我认为 glob 会更容易使用:

$files = glob("$argv[1]/*-*/*");

foreach($files as $file) {
    $parts = explode("/", $file);

    // get the directory part
    $f = explode("-", $parts[count($parts) - 2]);

    if(in_array($f[0], $servers) and in_array($f[1], $tests)) {
        echo "Test Found $f[0] in directory $argv[1]/$f[0]-$f[1]\n";
        echo "--$argv[1]/$f[0]-$f[1]/$sfile\n";
    }
}

Why are you using opendir at all? I think glob would be a little easier to use:

$files = glob("$argv[1]/*-*/*");

foreach($files as $file) {
    $parts = explode("/", $file);

    // get the directory part
    $f = explode("-", $parts[count($parts) - 2]);

    if(in_array($f[0], $servers) and in_array($f[1], $tests)) {
        echo "Test Found $f[0] in directory $argv[1]/$f[0]-$f[1]\n";
        echo "--$argv[1]/$f[0]-$f[1]/$sfile\n";
    }
}
多像笑话 2024-10-21 09:47:16

替换

while($sfile = readdir($sdir)){

while(($sfile = readdir($sdir)) !== 0){

否则,当文件名为 0 时,$sfile 为“0”,转换为 false。通过使用 !== 或 ===,您可以强制在变量之间进行类型检查,以便“0”不等于 0。

Replace

while($sfile = readdir($sdir)){

with

while(($sfile = readdir($sdir)) !== 0){

Otherwise when filename is 0, $sfile is "0" which is translated to false. By using !== or === you are forcing a type check between the variables so that "0" does not equal 0.

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