PHP RFC: StreamWrapper Support for glob()
- Version: 1.0
- Date: 2022-08-15
- Author: Timmy Almroth, firstname.lastname@example.org
- Status: Under Discussion
- First Published at: https://wiki.php.net/rfc/glob_streamwrapper_support
Comment: This RFC has been aborted as no convenient solution or workaround was found for Alpine platform. / Timmy
glob() is a function in PHP to list and filter files and/or folders using a globbing pattern. The current implementation of
glob() is a thin wrapper for POSIX glob(3). While all the Filesystem Functions in PHP supports StreamWrappers,
glob() does not.
Workarounds can be a struggle. Some projects may rely on regular expressions which are different from globbing. Some may attempt the use of fnmatch() which does not support all the features of glob() like brace expansions.
Consistently implement StreamWrapper support for glob(). Example:
Backward Incompatible Changes
Fully backwards compatibile
Proposed PHP Version(s)
Next PHP 8.x
The glob opendir implementation would be replaced with a wrapper supporting streams. It is possible to produce a fallback condition leaving local file system operations as is. But testing so far have not shown any performance anomalies or incompatibilities. So the intention is to also use the new wrapper for local filesystem operations.
open_basedir check will be removed from result filtering in favour of the already new implemented one in wrapper's opendir.
If a StreamWrapper supports listing contents, it will supported by glob(). There will be edge cases where a streamwrapper is not intended to or does not support listing contents of a resource, e.g. http:, data:, compress.zlib:. These cases are not unique to glob() as the same problemematics applies to scandir(). glob() returns an empty array for invalid paths today and will continue doing so for wrappers that can't be used to return a list of contents.
Which streamwrapper that can be used with glob() is not a limitation of glob(), but the streamwrapper itself. PHP's current ftp streamwrapper might not support listing files in a directory, but a 3rd party wrapper for ftp might.
A word of advice can be put in the documentation that clearifies how glob() will not work with these edge case wrappers. But again, this applies to the current implementation of scandir() also.
Unaffected PHP Functionality
glob(), GlobalIterator and glob:\\ for local filesystem will return the same results.
Wrappers not intended to return a list of contents will continue returning an empty array as glob does today.
Edit (Oct 2): As noted by @bukka; the use of GLOB_ALTDIRFUNC is a non-standard implementation that doesn't seem to be supported on Alpine. Meaning streamwrappers for glob() would remain unsupported on Alpine systems unless solved.
There has been some ideas about the future of PHP glob() and the maintained win32 implementation. Whether it could be replaced by a unified standalone implementation, rather than a POSIX layer for the local file system and a separate win32 implementation.
Proposed Voting Choices
Should we implement StreamWrapper support for PHP glob()?
Patches and Tests
A PoC has been produced in the Github Feature Request thread #9224. Final patch will be produced by Github user @KapitanOczywisty if this RFC is approved.
After the project is implemented, this section will contain
- the version(s) it was merged into
- a link to the git commit(s)
- a link to the PHP manual entry for the feature
- a link to the language specification section (if any)
Github Feature Request #9224