Re: How to escape a local path so fopen() doesn't consider it a URL?
| From: | Jesse Schalken | Date: | Wed, 21 Dec 2016 12:40:06 +0000 |
| Subject: | Re: How to escape a local path so fopen() doesn't consider it a URL? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-325911@lists.php.net to get a copy of this message | ||
On Wed, Dec 21, 2016 at 12:06 AM, Christoph M. Becker <cmbecker69@gmx.de>
wrote:
> 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
a
// 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;
}