6

I have issue while using glob function when path directory with square brackets.

// 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.

// 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>'; 

5 Answers 5

6

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>'; 
Sign up to request clarification or add additional context in comments.

1 Comment

Because DIRECTORY_SEPARATOR in Windows is \ too. Example of foo/bar/baz\[\]/qux.txt. Others operating systems will treat it as foo, bar, baz[], qux.txt but Windows will treats it as foo, bar, baz, [, ], qux.txt
4

Here's the one-liner that I use, that works great:

$path = str_replace(['[',']',"\f[","\f]"], ["\f[","\f]",'[[]','[]]'], $path); 

Note: The \f character is chosen as an unlikely to appear form-feed character as placeholder when replacing.

Comments

1

[foo] has a special meaning, it represents a character class (regular expression syntax).

So to have [ and ] mean square brackets literally, you have to escape them – by preceding them with a backslash.

1 Comment

not working with backslash also $path = 'temp'. DIRECTORY_SEPARATOR .'dir - [name]'; $path = str_replace('[', '\[', $path); $path = str_replace(']', '\]', $path);
0

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

1 Comment

This does not work. \[ and \] is not accepted by glob, but as suggested above, [[] and []] is accepted.
0

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:

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. Single-brackets some[folder and brackets without a char between some[]folder don't need to be escaped.

Usage example:

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

Tested on Linux and Windows.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.