6

NOTE: The pattern in question has been removed from the PHP documentation since this thread was created.

According to the PHP documentation, the pattern ... matches all subdirectories recursively, but when I try using it, no files are matched.

According to the documentation, glob hasn't changed since PHP 5.1, but if it matters, I am using PHP 7.2.24 .

Directory structure:

.
├── bar
│   └── bar_file
└── foo
    ├── 1
    │   └── foo_1_file
    └── foo_file

PHP:

var_dump(glob('./.../*')); // prints array(0) {}
var_dump(glob('./.../foo_file')); // prints array(0) {}

I know there is a workaround for this problem, but I would like to know if there is a PHP native solution or if there isn't, why the PHP reference documentation is defective.

eike
  • 1,314
  • 7
  • 18
  • 2
    I checked PHP bug tracker, it seems that there is nothing related. On the other hand, I didn't found any working example with the ... (three dots) pattern. – Jan Potužník Nov 19 '19 at 11:17
  • 1
    Not sure if this is just the documentation, as just above the *...* is *[...] - Matches one character from a group of characters. If the first character is !, matches any character not in the group*. This means they don't mean the literal *...* but just some content. – Nigel Ren Nov 19 '19 at 16:27
  • 1
    @NigelRen but if it was "just some content", wouldn't it also match files and not just subdirectories? – eike Nov 19 '19 at 17:24
  • 1
    I was merely pointing out that in the other example, the ... does not mean a literal ... but some other content. It may be just an [elipsis](https://www.thefreedictionary.com/Elipsis). – Nigel Ren Nov 19 '19 at 17:27
  • 1
    @NigelRen And I was merely stating, that the description of its function "... - Matches all the subdirectories, recursively." (and the fact that it is listed under special characters), to me, indicates that it is not to be interpreted as an elipsis. – eike Nov 19 '19 at 17:31

1 Answers1

5

The documentation is incomplete or even incorrect. As of Nov 2019, there is no code to explicitly support recursive glob syntax in PHP and the underlying operating system libraries are unlikely to support it either.

  1. There is no recursive glob syntax in IEEE 1003.1

  2. PHP UNIX implementation delegates to GLOB(3) from a standard C library. On Linux this will most likely be glibc which has no support for recursive syntax.

  3. PHP Windows implementation has no support for directory recursion

  4. None of the glob tests in PHP test suite include a test that covers a triple dot (...) syntax.

  5. According to the commit message of the change that introduced glob pattern syntax to PHP documentation, the list of special characters was based on the ones supported by djgpp libc library. The djgpp manpage states that the triple dot syntax is a nod to an old VMS feature.

    ... Matches all the subdirectories, recursively (VMS aficionados, rejoice!).

All of this is strong evidence that the recursive syntax listed in the documentation will not work unless PHP is running on a platform that has support for it, e.g. DJGPP on DOS or old Windows.

Community
  • 1
  • 1
anttix
  • 7,709
  • 1
  • 24
  • 25