#33047 [Com]: glob() doesn't work inside directories with brackets [ ]
| From: | tommytompkins at redcart dot com | Date: | Mon, 16 Mar 2009 22:10:09 +0000 |
| Subject: | #33047 [Com]: glob() doesn't work inside directories with brackets [ ] | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-134777@lists.php.net to get a copy of this message | ||
ID: 33047
Comment by: tommytompkins at redcart dot com
Reported By: wiseman1024 at gmail dot com
Status: No Feedback
Bug Type: Directory function related
Operating System: Windows 2000
PHP Version: 4.3.9
New Comment:
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 . "\.?*");
Previous Comments:
------------------------------------------------------------------------
[2007-09-18 15:42:26] 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 . '*'));
------------------------------------------------------------------------
[2006-02-16 23:52:50] 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;
------------------------------------------------------------------------
[2006-02-16 23:19:14] 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 18:41:20] 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;
------------------------------------------------------------------------
[2005-05-25 01:00:04] 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".
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/33047
--
Edit this bug report at http://bugs.php.net/?id=33047&edit=1