【问题标题】:Ignore hidden files with php [duplicate]用php忽略隐藏文件[重复]
【发布时间】:2011-03-29 20:05:22
【问题描述】:

我正在尝试扫描图像文件夹,但是我一直看到 mac 创建的 ._ 文件

我正在使用此代码:

   <?php
if ($handle = opendir('assets/automotive')) {
    $ignore = array( 'cgi-bin', '.', '..','._' );
    while (false !== ($file = readdir($handle))) {
        if ( !in_array($file,$ignore)) {
            echo "$file\n";
        }
    }
    closedir($handle);
}
?>

关于为什么的任何想法?我创建了一个覆盖它的忽略数组。

更新:仍然显示两者。

【问题讨论】:

    标签: php arrays opendir


    【解决方案1】:

    我认为您想忽略任何以点 (.) 开头的文件 而不仅仅是文件名。

    <?php
    if ($handle = opendir('assets/automotive')) {
        $ignore = array( 'cgi-bin', '.', '..','._' );
        while (false !== ($file = readdir($handle))) {
            if (!in_array($file,$ignore) and substr($file, 0, 1) != '.') {
                echo "$file\n";
            }
        }
        closedir($handle);
    }
    ?>
    

    【讨论】:

    • 是的,这就是诀窍,谢谢!当它允许我时,我会将其标记为已接受
    • 鉴于我们知道$file 是一个非空字符串,substr($file, 0, 1) 是编写$file[0] 的一种不必要的冗长方式。让$ignore 数组包含点文件的冗余尽管它们被其他条件覆盖,但也很丑陋。
    【解决方案2】:

    in_array()takes two parameters:你要查找的东西,以及要查找的数组。 你想要:

    if ( !in_array($file, $ignore))
    

    【讨论】:

    • 我添加了,仍然显示两种类型
    • 我运行了类似的代码,它对我来说可以正常工作。
    【解决方案3】:

    您正在检查in_array,但接下来的问题是:“是 what in_array”。

    in_array 需要第二个参数,在本例中为 $file,以查找。你需要:

    in_array($file,$ignore);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      • 2023-03-23
      • 2015-08-25
      • 2011-09-27
      • 2012-09-25
      • 1970-01-01
      相关资源
      最近更新 更多