方括号目录中的PHP glob() [英] PHP glob() in bracketed directories

查看:84
本文介绍了方括号目录中的PHP glob()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows计算机上,以下脚本:

On a Windows machine, the following script:

<?php

mkdir("c:\\[test]");
file_put_contents("c:\\[test]\\test.txt", "some content");
chdir("c:\\[test]");
echo getcwd()."\n";
var_dump(glob('*'));

?>

显示此:

C:\[test]
array(0) {
}

在预期的情况下:

C:\[test]
array(1) {
    [0]=>
    string(8) "test.txt"
}

我理解globpattern参数中找到时会将方括号视为特殊字符.

模式*与当前工作目录中的任何文件匹配.但是,glob() 行为就像是使用模式c:\\[test]\\*

The pattern * matches any file in the current working directory. However, glob() behaves as though it was run with the pattern c:\\[test]\\*

然后将方括号解释为模式的一部分,而实际上 是目录的一部分.

The brackets are then being interpreted as part of the pattern, when in fact they are part of the directory.

glob是否应该将 path 视为 pattern 的一部分?我宁愿认为它应将当前目录用作起点,然后仅处理模式.

Is glob supposed to treat the path as part of the pattern? I would rather think it should use the current directory as a starting point, and then process the pattern only.

(尝试进行总结):glob函数的作用类似于将c:\\[test]\\*作为匹配模式,并试图匹配c:\t\*c:\e\*c:\s\*.但是模式实际上是*,它不应该尝试匹配任何一个.

(Attempt to summarize): The glob function acts like it's getting c:\\[test]\\* as a match pattern, and is trying to match either c:\t\*, c:\e\*, or c:\s\*. But the pattern is actually * and it shouldn't be trying to match any of that.

推荐答案

在php.net上的此错误报告中,这似乎已作为一个问题加以解决:

This seems to be covered as an issue in this bug report on php.net: https://bugs.php.net/bug.php?id=33047

该线程上的最后一篇文章不是关于bug,而是关于glob如何将括号作为正则表达式的一部分来解决的问题.我不确定我是否同意.除非您不能上移到父文件夹,否则似乎可以解决此问题.

The last post on that thread is about it not being a bug, but an issue from how glob treats brackets, as part of the regular expression. I'm not sure I agree. It seems you can work around this, unless you cannot move up into the parent folder.

如果删除位于[test]文件夹中的第一个条件,则可以使用以下语法获取文件列表:

If you remove the first requirement of being in the inside [test] folder, you can get the file listing by using a syntax like below:

chdir('..');
$glob = glob("[[]test[]]/*");

鉴于这些复杂性,如果您在Windows计算机上遇到问题,建议不要使用glob函数,并查看其他文件列表功能,例如

Given these complications, I would recommend not using the glob function if you are running into issues on windows machines, and look at other file listing functions like readdir.

这篇关于方括号目录中的PHP glob()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆