php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48365 glob() function returns empty array
Submitted: 2009-05-22 19:38 UTC Modified: 2009-09-26 01:00 UTC
Votes:5
Avg. Score:4.6 ± 0.8
Reproduced:4 of 5 (80.0%)
Same Version:3 (75.0%)
Same OS:1 (25.0%)
From: peter dot chen at itg dot com Assigned:
Status: No Feedback Package: Filesystem function related
PHP Version: 5.2.9 OS: sparc-sol2.10
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2009-05-22 19:38 UTC] peter dot chen at itg dot com
Description:
------------
I am trying to use glob() to display the contents of a directory
I store the path in a variable ($path). 

When I give $path as an argument to glob($path), it returns an empty array.

When I give it to scandir($path), it returns all the files in that path. 

Reproduce code:
---------------
---
From manual page: function.glob
---


Expected result:
----------------
Array ( [0] => . [1] => .. [2] => authtest [3] => globtest.php [4] => globtest.php.LCK [5] => globtest2.php [6] => home.php.LCK ) 

Actual result:
--------------
Array ( ) 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-22 19:43 UTC] jani@php.net
Please provide proper example script.
 [2009-05-22 21:09 UTC] peter dot chen at itg dot com
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>glob test</title>
</head>
<body>
<?
   $path = "/home/apache/htdocs/testkt/";
     
    print("<b>glob:</b><br><br>");
    print_r(glob($path . '*.*';));
	
	print("<br><br>***********************<br><br>");
	
	print("<b>scandir:</b><br><br>");
    print_r(scandir($path));
?>
</body>
</html>
 [2009-05-23 10:35 UTC] carsten_sttgt at gmx dot de
> print_r(glob($path . '*.*';));

This line give you a parse error. Must be:
| print_r(glob($path . '*.*'));

Apart from this, the code is working for me (Win,BSD).

BTW:
> [0] => .
> [1] => ..
> [2] => authtest

With your code, this can't be part of the result from glob().

Regards,
Carsten
 [2009-05-27 23:05 UTC] peter dot chen at itg dot com
My apologies, I did not read all the instructions. 

Is there a flag I needed to enable when building php?

As for the code, I mistakenly left the ';' that caused the parse error. The code should be as follows: 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>glob test</title>
</head>
<body>
<?
   $path = "/home/apache/htdocs/testkt/";
     
    print("<b>glob:</b><br><br>");
    print_r(glob($path . '*.*'));
	
	print("<br><br>***********************<br><br>");
	
	print("<b>scandir:</b><br><br>");
    print_r(scandir($path));
?>
</body>
</html>
 [2009-05-28 16:49 UTC] jani@php.net
Exactly what Solaris version are you on?
Try this on command line:

# php -r 'var_dump(glob("/home/apache/htdocs/testkt/"));'

 [2009-05-28 18:42 UTC] peter dot chen at itg dot com
I'm using sparc-solaris2.10. 

Was there a flag I needed to include for my php install to enable glob() to work?
 [2009-05-28 18:49 UTC] peter dot chen at itg dot com
Oh yeah, and the command returns:

bool(false)
 [2009-05-29 12:05 UTC] jani@php.net
Is all dirs in that path readable by the user you run it with? (try as root..?) glob() returns false if there's some error..
 [2009-05-29 14:55 UTC] peter dot chen at itg dot com
Yes, that path's permissions are 755 and every file in that directory can be read by all.

I've run it as root also, same return value.
 [2009-05-31 15:48 UTC] jani@php.net
And all that path is on local filesystem and not on some e.g. NFS share?
 [2009-06-01 17:09 UTC] peter dot chen at itg dot com
My mistake. Upon closer inspection, I realized that the path I specified was incorrect. 

This is the result after I corrected the path: 
array(0) {
}
 [2009-06-01 22:04 UTC] peter dot chen at itg dot com
Actually, now that you mention it, the path is on an NFS.
 [2009-06-02 08:25 UTC] jani@php.net
Try compile and run this small C program:

------------------------

#include <glob.h>
#include <stdio.h>

int main()
{
  glob_t globbuf;
  int i;

  globbuf.gl_offs = 15;

  glob("/home/apache/htdocs/testkt/*", GLOB_MARK, NULL, &globbuf);

  for(i = 0; i < globbuf.gl_pathc; i++)
    printf("%s\n",globbuf.gl_pathv[i]);

  globfree(&globbuf);
  return(0);
}

------------------------

See also bug #22641 

 [2009-06-04 01:25 UTC] peter dot chen at itg dot com
The .c code worked. 

gcc version 4.2.4
 [2009-06-10 13:01 UTC] jani@php.net
And what did the compiled program output?
 [2009-06-10 16:33 UTC] peter dot chen at itg dot com
It returned: 

/home/apache/htdocs/testkt/authtest/
/home/apache/htdocs/testkt/globtest.php
/home/apache/htdocs/testkt/globtest.php.LCK
/home/apache/htdocs/testkt/globtest2.php
/home/apache/htdocs/testkt/home.php.LCK

which is all the contents within testkt/
 [2009-09-18 19:06 UTC] jani@php.net
Please try using this snapshot:

  http://snaps.php.net/php5.3-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2009-09-26 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".
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue May 11 00:01:23 2021 UTC