Current Location: Home> Latest Articles> Can stream_get_meta_data recognize the stream protocol type? Usage explained

Can stream_get_meta_data recognize the stream protocol type? Usage explained

gitbox 2025-07-17

In PHP, stream_get_meta_data() is a very useful function that retrieves various metadata related to streams. Many developers are curious when using this function:

This article will analyze this question and demonstrate the actual functionality and limitations of stream_get_meta_data() through usage examples.

Can stream_get_meta_data() recognize the protocol type?

The short answer is: it cannot directly recognize the protocol type.

Although stream_get_meta_data() returns many useful pieces of information, such as whether the end of the file has been reached (eof), whether the stream is blocked (blocked), and whether it is readable/writable, it does not directly return the protocol type of the stream. The protocol type information is actually implicit in the stream resource when it is created but is not exposed through this function.

For example:

<span><span><span class="hljs-variable">$fp</span></span><span> = </span><span><span class="hljs-title function_ invoke__">fopen</span></span><span>(</span><span><span class="hljs-string">"http://example.com"</span></span><span>, </span><span><span class="hljs-string">"r"</span></span><span>);
</span><span><span class="hljs-variable">$meta</span></span><span> = </span><span><span class="hljs-title function_ invoke__">stream_get_meta_data</span></span><span>(</span><span><span class="hljs-variable">$fp</span></span><span>);
<p></span>print_r($meta);<br>
fclose($fp);<br>
</span>

The returned $meta data structure looks roughly like this:

<span><span><span class="hljs-title function_ invoke__">Array</span></span><span>
(
    [wrapper_type] => http
    [stream_type] => tcp_socket
    [mode] => r
    [unread_bytes] => </span><span><span class="hljs-number">0</span></span><span>
    [seekable] => 
    [uri] => </span><span><span class="hljs-attr">http</span></span><span>://example.com
    [timed_out] => 
    [blocked] => </span><span><span class="hljs-number">1</span></span><span>
    [eof] => 
)
</span></span>

The wrapper_type field is the closest to a “protocol type” field, such as http, ftp, php, and so on. Although this is not specifically designed for protocol recognition, we can indirectly determine the protocol used by the stream from it.

The meaning of wrapper_type and stream_type

  • wrapper_type: This field indicates the type of stream wrapper, which is the “wrapper” PHP uses, often related to the protocol. Common values include http, ftp, php, plainfile, etc.

  • stream_type: A lower-level representation such as tcp_socket, ssl, STDIO, reflecting the actual transmission method.

If you want to identify the protocol layer, it’s recommended to check the wrapper_type field first.

How to determine the protocol through wrapper_type?

You can encapsulate a helper function to extract and return the protocol information. For example:

<span><span><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span> </span><span><span class="hljs-title">getStreamProtocol</span></span><span>(</span><span><span class="hljs-variable">$stream</span></span><span>) {
    </span><span><span class="hljs-variable">$meta</span></span><span> = </span><span><span class="hljs-title function_ invoke__">stream_get_meta_data</span></span><span>(</span><span><span class="hljs-variable">$stream</span></span><span>);
    </span><span><span class="hljs-keyword">return</span></span><span> </span><span><span class="hljs-variable">$meta</span></span><span>[</span><span><span class="hljs-string">&#039;wrapper_type&#039;</span></span><span>] ?? </span><span><span class="hljs-literal">null</span></span><span>;
}
</span></span>

Usage example:

<span><span><span class="hljs-variable">$stream</span></span><span> = </span><span><span class="hljs-title function_ invoke__">fopen</span></span><span>(</span><span><span class="hljs-string">&#039;php://input&#039;</span></span><span>, </span><span><span class="hljs-string">&#039;r&#039;</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">getStreamProtocol</span></span><span>(</span><span><span class="hljs-variable">$stream</span></span><span>); </span><span><span class="hljs-comment">// Output: php</span></span><span>
</span><span><span class="hljs-title function_ invoke__">fclose</span></span><span>(</span><span><span class="hljs-variable">$stream</span></span><span>);
</span></span>

This way, you can basically determine the protocol source of the stream.

Summary

Although stream_get_meta_data() does not explicitly return the protocol name, the returned wrapper_type field is sufficient to help developers identify the protocol or wrapper type used. This indirect recognition method is already very practical in real-world development.

Tip: When handling multiple types of stream resources, be sure to consider compatibility and exception handling, especially when dealing with network or remote streams.