Skip to content

Stream context is lost when custom stream wrapper is being filtered #15155

Description

@qkdreyer

Description

The following code:

<?php

class DummyWrapper
{
    public $context;
    
    public static function register(): void
    {
        \stream_wrapper_register('dummy', self::class);
    }
    
    public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool
    {
        $options = \stream_context_get_options($this->context);
        var_dump($options['dummy']['foo']);
        
        return true;
    }
    
    public function stream_stat()
    {
    }
    
    public function stream_read()
    {
    }
    
    public function stream_eof()
    {
    }
}

DummyWrapper::register();
\file_get_contents('dummy://foo', false, \stream_context_create(['dummy' => ['foo' => 'bar']]));
\file_get_contents('php://filter/resource=dummy://foo', false, \stream_context_create(['dummy' => ['foo' => 'bar']]));

Resulted in this output:

string(3) "bar"

Fatal error: Uncaught TypeError: stream_context_get_options(): Argument #1 ($stream_or_context) must be of type resource, null given in /in/hlqFE:14
Stack trace:
#0 /in/hlqFE(14): stream_context_get_options(NULL)
#1 [internal function]: DummyWrapper->stream_open('dummy://foo', 'rb', 0, NULL)
#2 /in/hlqFE(35): file_get_contents('php://filter/re...', false, Resource id #7)
#3 {main}
  thrown in /in/hlqFE on line 14

But I expected this output instead:

string(3) "bar"
string(3) "bar"

PHP Version

PHP 8.3.9

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions