Web lists-archives.com

[PHP] Re: How to escape a local path so fopen() doesn't consider it a URL?

On Wed, Dec 21, 2016 at 12:06 AM, Christoph M. Becker <cmbecker69@xxxxxx>

> On 20.12.2016 at 07:58, Jesse Schalken wrote:
> Use an absolute path, or at least some qualification, such as
> `./data:foo`.  That is sufficient to tell PHP that you're not trying to
> access a data URL.
Thanks. I managed to find the responsible functions in PHP
(php_stream_locate_url_wrapper) and HHVM (Stream::getWrapperProtocol) and
came up with this, which seems to work:

 * Make sure a path will be treated as a local file path and not as a URL
 * for some other stream wrapper.
function make_path_local(string $path): string {
  // Look at php_stream_locate_url_wrapper() in PHP source
  // or Stream::getWrapperProtocol() in HHVM
  // Basically, any path that matches this regex is likely to be considered
  // URL for another stream wrapper.
  // Handily, a path that matches this regex is guaranteed not to be an
  // absolute path on POSIX or Windows, so if it matches we can safely
  // force it not to match by prefixing it with ./ on POSIX and .\ on
  // Windows.

  if (\preg_match('/^([a-zA-Z+\\-.]+:\\/\\/|data:|zlib:)/Ds', $path))
    return '.' . \DIRECTORY_SEPARATOR . $path;

  return $path;