简体   繁体   English

当目录名称带有方括号“[]”等特殊字符时,Glob 不起作用

[英]Glob is not working when directory name with special characters like square brackets "[ ]"

I have issue while using glob function when path directory with square brackets.当路径目录带有方括号时,我在使用 glob function 时遇到问题。

// Example 1 - working
$path = 'temp'. DIRECTORY_SEPARATOR .'dir - name';
$files = glob($path . DIRECTORY_SEPARATOR . '*.txt');
// List all files 
echo '<pre>';
    print_r($files);
echo '</pre>';

Above code is working but when directory name with square brackets like dir[name] or dir - [name] then its not working.上面的代码有效,但是当目录名称带有方括号,如 dir[name] 或 dir - [name] 时,它就不起作用了。

// Example 2 - not working
$path = 'temp'. DIRECTORY_SEPARATOR .'dir - [name]';
$files = glob($path . DIRECTORY_SEPARATOR . '*.txt');
// result got empty if file on that directory 
echo '<pre>';
    print_r($files);
echo '</pre>';

Thanks for all of you. 谢谢你们所有人。

I got exact solution for my query. 我得到了我的查询的确切解决方案。 below code is a working for me 下面的代码对我有用

$path = 'temp'. DIRECTORY_SEPARATOR .'dir - [name]';
$path = str_replace('[', '\[', $path);
$path = str_replace(']', '\]', $path);
$path = str_replace('\[', '[[]', $path);
$path = str_replace('\]', '[]]', $path);
$files = glob($path . DIRECTORY_SEPARATOR . '*.txt');
// List files
echo '<pre>';
    print_r($files);
echo '</pre>';

This is what I use: 这是我使用的:
$path = str_replace(['[',']',"\\f[","\\f]"], ["\\f[","\\f]",'[[]','[]]'], $path);

All in one line. 全部在一条线上。

[foo] has a special meaning, it represents a character class (regular expression syntax). [foo]具有特殊含义,它表示一个字符类(正则表达式语法)。

So to have [ and ] mean square brackets literally , you have to escape them – by preceding them with a backslash. 因此有[]意味着方括号字面上看 ,你已经逃离他们-用一个反斜杠之前他们。

Try 尝试

$path = 'temp'. DIRECTORY_SEPARATOR .'dir - [name]';
$from = array('[',']');
$to   = array('\[','\]');
$files =glob(str_replace($from,$to,$path . "\\*.txt"));
echo '<pre>';
    print_r($files);
echo '</pre>';

Late to the party I know, but based on previous answers, and after testing on both Linux/Mac and Windows, I came up with this utility function:我知道晚会迟到了,但根据之前的回答,并在 Linux/Mac 和 Windows 上测试后,我想出了这个实用程序 function:

function glob_escape($path){
  return preg_match('/\[.+]/', $path) ? str_replace(['[',']', '\[', '\]'], ['\[','\]', '[[]', '[]]'], $path) : $path;
}

For practical reasons, the function only attempts to escape characters when there are opening and closing [brackets] in the $path , with minimum one character in between.出于实际原因,function 仅在$path中有左右[brackets]时才尝试转义字符,中间至少有一个字符。 Single-brackets some[folder and brackets without a char between some[]folder don't need to be escaped.单括号some[folder和括号之间没有 char 的some[]folder不需要转义。

Usage example:使用示例:

$path = 'some/dir[brackets]here/more[brackets]blah';
glob(glob_escape($path) . '/*', GLOB_NOSORT|GLOB_ONLYDIR);

Tested on Linux and Windows.在 Linux 和 Windows 上测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM