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

发布于 2024-07-29 21:00:23 字数 760 浏览 6 评论 0原文

我有名称中包含括号的目录。 即“dir_123[[email protected]]”

在该目录中有 . tif 文件。

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

$anz = count(glob(str_replace("[", "\[", "dir_123[[email protected]]/*.tif")));

在运行 XAMPP 的 Windows 计算机上,由于括号,它无法工作:

$anz = count(glob(str_replace("[", "\[", "dir_123[[email protected]]\\*.tif")));

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

I have got directories which contain brackets in the names.
i.e. "dir_123[[email protected]]"

Within that dirs there are .tif files.

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

$anz = count(glob(str_replace("[", "\[", "dir_123[[email protected]]/*.tif")));

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

$anz = count(glob(str_replace("[", "\[", "dir_123[[email protected]]\\*.tif")));

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

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

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

发布评论

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

评论(3

笑,眼淚并存 2024-08-05 21:00:25

我通过使用以下代码解决了这个问题:

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

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);
捶死心动 2024-08-05 21:00:24

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

所以,正确的转义是:

$pattern = "dir_123[[email protected]]/*.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 ));

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

So, correct escaping is:

$pattern = "dir_123[[email protected]]/*.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 ));
葮薆情 2024-08-05 21:00:23

您是否尝试过转义所有特殊字符?

前任。

$dir = "dir_123[[email protected]]";

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

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

这对我在 Ubuntu 上有效。

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

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

如果你需要递归计数:

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;
}

这个函数已经过测试,并在我的 Ubuntu 9.04 计算机上使用 php 5.2.6-3ubuntu4.1

希望它对你有用!

//莱纳斯·温内贝克

Have you tried to escape all the special characters?

Ex.

$dir = "dir_123[[email protected]]";

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

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

This works for me on 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

Hope it works for ya!

//Linus Unnebäck

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