使用glob()查找名称中带括号的目录

时间:2022-09-01 23:13:20

I have got directories which contain brackets in the names. i.e. "dir_123[test@test.de]"

我有名字中包含括号的目录。即“dir_123 [test@test.de]”

Within that dirs there are .tif files.

在那些目录中有.tif文件。

What I do is counting the Tif files. On my Mac I did that with MAMP and it worked great:

我所做的是计算Tif文件。在我的Mac上,我使用MAMP做到了,效果很好:

$anz = count(glob(str_replace("[", "\[", "dir_123[test@test.de]/*.tif")));

On my Windows machine running XAMPP it won't work because of that brackets:

在运行XAMPP的Windows机器上,由于这些括号,它将无法工作:

$anz = count(glob(str_replace("[", "\[", "dir_123[test@test.de]\\*.tif")));

How can I get my XAMPP Server to read that directories?

如何让我的XAMPP服务器读取该目录?

3 个解决方案

#1


Have you tried to escape all the special characters?

你试图逃脱所有特殊角色吗?

Ex.

$dir = "dir_123[test@test.de]";

$from = array('[',']');
$to   = array('\[','\]');

$anz = count(glob(str_replace($from,$to,$dir . "\\*.tif")));

This works for me on Ubuntu.

这适用于Ubuntu。

If that ain't working you can do:

如果这不起作用,你可以这样做:

function countTif($dir) {
    $ret = 0;
    $scan = scandir($dir);
    foreach($scan as $cur) {
        $ret += ((substr($cur,-4) == ".tif")?1:0);
    }
    return $ret;
}

And if you need recursive counting:

如果你需要递归计数:

function countTif($dir) {
    $ret = 0;
    $scan = scandir($dir);
    foreach($scan as $cur) {
        if(is_dir("$dir/$cur") and !in_array($cur,array('.','..'))) {
            $ret += countTif("$dir/$cur");
        } else {
            $ret += ((substr($cur,-4) == ".tif")?1:0);
        }
    }
    return $ret;
}

This functions was tested and worked on my Ubuntu 9.04 computer with php 5.2.6-3ubuntu4.1

这个函数在我的Ubuntu 9.04计算机上使用php 5.2.6-3ubuntu4.1进行了测试和工作

Hope it works for ya!

希望它适用于你!

//Linus Unnebäck

#2


Visit http://unixhelp.ed.ac.uk/CGI/man-cgi?glob+7 for understanding wildcards for glob().

访问http://unixhelp.ed.ac.uk/CGI/man-cgi?glob+7以了解glob()的通配符。

So, correct escaping is:

所以,正确的逃避是:

$pattern = "dir_123[test@test.de]/*.tif";
if ( strpos($pattern, '[') !== false || strpos($pattern, ']') !== false )
{
    $placeholder = uniqid();
    $replaces = array( $placeholder.'[', $placeholder.']', );
    $pattern = str_replace( array('[', ']', ), $replaces, $pattern);
    $pattern = str_replace( $replaces, array('[[]', '[]]', ), $pattern);
}
$anz = count(glob( $pattern ));

#3


I solved it by using this code instead:

我通过使用此代码解决了它:

$dir = scandir("\\\\server\\dir");
foreach ($dir as $key=>$row)
    if (end(explode(".", $row)) != "tif")
        unset($dir[$key]);
$anz = count($dir);

#1


Have you tried to escape all the special characters?

你试图逃脱所有特殊角色吗?

Ex.

$dir = "dir_123[test@test.de]";

$from = array('[',']');
$to   = array('\[','\]');

$anz = count(glob(str_replace($from,$to,$dir . "\\*.tif")));

This works for me on Ubuntu.

这适用于Ubuntu。

If that ain't working you can do:

如果这不起作用,你可以这样做:

function countTif($dir) {
    $ret = 0;
    $scan = scandir($dir);
    foreach($scan as $cur) {
        $ret += ((substr($cur,-4) == ".tif")?1:0);
    }
    return $ret;
}

And if you need recursive counting:

如果你需要递归计数:

function countTif($dir) {
    $ret = 0;
    $scan = scandir($dir);
    foreach($scan as $cur) {
        if(is_dir("$dir/$cur") and !in_array($cur,array('.','..'))) {
            $ret += countTif("$dir/$cur");
        } else {
            $ret += ((substr($cur,-4) == ".tif")?1:0);
        }
    }
    return $ret;
}

This functions was tested and worked on my Ubuntu 9.04 computer with php 5.2.6-3ubuntu4.1

这个函数在我的Ubuntu 9.04计算机上使用php 5.2.6-3ubuntu4.1进行了测试和工作

Hope it works for ya!

希望它适用于你!

//Linus Unnebäck

#2


Visit http://unixhelp.ed.ac.uk/CGI/man-cgi?glob+7 for understanding wildcards for glob().

访问http://unixhelp.ed.ac.uk/CGI/man-cgi?glob+7以了解glob()的通配符。

So, correct escaping is:

所以,正确的逃避是:

$pattern = "dir_123[test@test.de]/*.tif";
if ( strpos($pattern, '[') !== false || strpos($pattern, ']') !== false )
{
    $placeholder = uniqid();
    $replaces = array( $placeholder.'[', $placeholder.']', );
    $pattern = str_replace( array('[', ']', ), $replaces, $pattern);
    $pattern = str_replace( $replaces, array('[[]', '[]]', ), $pattern);
}
$anz = count(glob( $pattern ));

#3


I solved it by using this code instead:

我通过使用此代码解决了它:

$dir = scandir("\\\\server\\dir");
foreach ($dir as $key=>$row)
    if (end(explode(".", $row)) != "tif")
        unset($dir[$key]);
$anz = count($dir);