php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33047 glob() doesn't work inside directories with brackets [ ]
Submitted: 2005-05-17 18:09 UTC Modified: 2005-05-25 01:00 UTC
Votes:17
Avg. Score:4.5 ± 0.8
Reproduced:17 of 17 (100.0%)
Same Version:1 (5.9%)
Same OS:5 (29.4%)
From: wiseman1024 at gmail dot com Assigned:
Status: No Feedback Package: Directory function related
PHP Version: 4.3.9 OS: Windows 2000
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
25 + 14 = ?
Subscribe to this entry?

 
 [2005-05-17 18:09 UTC] wiseman1024 at gmail dot com
Description:
------------
glob() won't work (with any pattern and options whatsoever) when one directory in the current working path has matching brackets [ and ] with characters between the brackets.

This means glob() will fail if the current working directory is one of:
C:\php\[hello]
C:\php\[hello]\hello
C:\php\xxx[hello]xxx
C:\php\[hello][

It will work, for example, if the current working directory is one of:
C:\php
C:\php\[hello
C:\php\hello]
C:\php\hello[]

This seems to be a problem when using regular expressions in the implementation of glob.

Reproduce code:
---------------
/* Returns a list of files and directories, unless inside a directory with something between brackets*/

print_r(glob('*',FALSE)); 

Expected result:
----------------
Should always return an array with the files (assuming there are files in the current directory)

Actual result:
--------------
Nothing, because glob() returned false

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-05-25 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-02-16 18:41 UTC] ian at res-alian dot com
I get the same problem with glob() using PHP 5.0.2 and 5.1.2 in Windows 2000 (my work computer, NTFS) and in Windows XP (notebook, 5.0.5, NTFS) but not Linux (server, 5.0.2, ext3). Sorry I can't offer a solution other than using Linux. I'm not sure if it has to do with the NTFS file system.

It took me a while to figure out why some folders didn't work, and I thought it was a permission thing. When I noticed the folder that worked had no bracket, I tried adding brackets, and that made it not work. (Also removing the brackets made ones with brackets suddenly work.) Then I knew what to look for and found this bug entry with Google.

What's strange is that glob() has no problem returning a list with some entries containing brackets, but not any entries at all when within a folder with brackets.

The other strange thing is the opendir() and readdir() work fine in the same situation under Windows. It's more inconvenient but serves as a workaround.

--i;
 [2006-02-16 23:19 UTC] ian at res-alian dot com
Here's my workaround code, using opendir() and readdir() (using code from the readdir() page on this site):

    if ($dirh = opendir("."))
    {
      $picfns = 0;

      while (false !== ($file = readdir($dirh)))
      {
        if (preg_match("/^_.*jpg$/", $file))
        {
          $file = preg_replace("/^_/", "", $file);

          if ($picfns)
          {
            array_push($picfns, $file);
          }
          else
          {
            $picfns = array($file);
          }
        }
      }

      closedir($dirh);
    }

replaces this code:

    $thumbfns = glob("_*.jpg");
    $jpgfns = glob("*.jpg");
    $picfns = array_diff($jpgfns, $thumbfns);

--i;
 [2006-02-16 23:52 UTC] ian at res-alian dot com
Argh. I forgot to add sort($picfns) for good measure, since readdir() returns entries in the order they appear on the hard drive while glob() returns them sorted (unless using the GLOB_NOSORT flag). If the PHP developers fix the glob() Win32 bug, I can stop ranting.... =)

--i;
 [2007-09-18 15:42 UTC] rele at gmx dot de
This bug has still not been fixed, I tested it with PHP 5.2.4 on WinXP SP 2.

In addition the bug occurs when the glob pattern parameter contains a directory path containing non-empty brackets.

And in contrary to the workaround mentioned in the manual, escaping does not change anything.
http://www.php.net/manual/en/function.glob.php#76621


chdir('C:\\');
$dir = 'C:\a[test]';
$dir_escaped = 'C:\a\[test\]';
print_r(glob($dir . DIRECTORY_SEPARATOR . '*'));
print_r(glob($dir_escaped . DIRECTORY_SEPARATOR . '*'));
 [2009-03-16 22:10 UTC] tommytompkins at redcart dot com
This actually is not a bug.  The glob() function is an extension of the Unix glob function and square brackets have special meaning with that function.

More information on the Unix glob() function can be found here:

http://unixhelp.ed.ac.uk/CGI/man-cgi?glob

Since brackets have special meaning with that function, you need to put brackets around the brackets.  For example you could do this as a work around:

/* First, replace all brackets with an escaped bracket */
$escapedBracketPath = str_replace('[', '\[', $path);
$escapedBracketPath = str_replace(']', '\]', $escapedBracketPath);
   
/* Next, replace all "escaped" brackets with brackets like so */
$escapedBracketPath = str_replace('\[', '[[]', $escapedBracketPath);
$escapedBracketPath = str_replace('\]', '[]]', $escapedBracketPath);
   
$normal_files = glob($escapedBracketPath . "*");
$hidden_files = glob($escapedBracketPath . "\.?*");
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 05:01:29 2024 UTC