Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glob pattern does not match anything

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.

like image 778
eike Avatar asked Nov 14 '19 09:11

eike


People also ask

What is glob style matching?

What are globs? Globs, also known as glob patterns are patterns that can expand a wildcard pattern into a list of pathnames that match the given pattern.

How does glob work in Python?

glob (short for global) is used to return all file paths that match a specific pattern. We can use glob to search for a specific file pattern, or perhaps more usefully, search for files where the filename matches a certain pattern by using wildcard characters.

What does glob command do?

This command performs file name “globbing” in a fashion similar to the csh shell or bash shell. It returns a list of the files whose names match any of the pattern arguments. No particular order is guaranteed in the list, so if a sorted list is required the caller should use lsort.

How do you implement Globbing?

The obvious implementation of glob pattern matching against a single path element is to walk the pattern and the name together, matching letters or wildcards in the pattern to letters in the name. If the walk reaches the end of the pattern at the same time as the end of the name, they match.


1 Answers

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.

like image 146
anttix Avatar answered Oct 21 '22 22:10

anttix