glob()有否定吗?

时间:2009-12-09 23:01:39

标签: php glob

我知道我可以这样做......

glob('/dir/somewhere/*.zip');

...让所有文件以.zip结尾,但有没有办法返回 ZIP的所有文件?

或者我应该迭代并过滤掉那个扩展名?

5 个答案:

答案 0 :(得分:24)

你可以尝试这样的事情:

$all = glob('/dir/somewhere/*.*');
$zip = glob('/dir/somewhere/*.zip');
$remaining = array_diff($all, $zip);

尽管使用Pascal提到的其他方法之一可能更有效。

答案 1 :(得分:16)

快速方法是glob()用于所有内容,并使用preg_grep()过滤掉您不想要的文件。

preg_grep('#\.zip$#', glob('/dir/somewhere/*'), PREG_GREP_INVERT)

另见Glob Patterns for File Matching in PHP

答案 2 :(得分:9)

这种模式可行:

glob('/dir/somewhere/*.{?,??,[!z][!i][!p]*}', GLOB_BRACE);

在/ dir / somewhere /中以点结尾后跟

查找所有内容
  • 一个字符(?
  • 或两个字符(??
  • 或任何不以连续字母z,i,p([!z][!i][!p]*
  • 开头的内容

答案 3 :(得分:8)

我认为glob不能做“非通配符”......

我至少看到了两个其他解决方案:

答案 4 :(得分:0)

$dir = "/path";
if (is_dir($dir)) {
    if ($d = opendir($dir)) {
           while (($file = readdir($d)) !== false) {
                if ( substr($file, -3, 3) != "zip" ){
                    echo "filename: $file \n";
                }
           }
        closedir($d);
    }
}

注意:“。”和“..”没有照顾。留给OP完成