Skip to content

glob() fails if square bracket is in current directory #13204

@dg

Description

@dg

Description

Create a file c:\test[x]\test.php (yes, it is important that it is in the directory named test[x]):

<?php
var_dump(glob('*'));

Resulted in this output:

array(0) {
}

But I expected this output instead:

array(1) {
  [0]=>
  string(8) "test.php"
}

The problem is that glob performs the expansion of [...] characters in the mask. The mask does not contain these characters, but since it is not absolute, the current directory is added to it:

snprintf(work_pattern, MAXPATHLEN, "%s%c%s", cwd, DEFAULT_SLASH, pattern);

And it contains these characters, so an unwanted expansion occurs. It is therefore necessary to escape these characters. At least the character [ must escaped, ie. replaced with [[] sequence.

PHP Version

PHP 8.3

Operating System

Windows

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions