Current Location: Home> Latest Articles> stream_get_filters is used in conjunction with stream_get_meta_data to get detailed information about the stream

stream_get_filters is used in conjunction with stream_get_meta_data to get detailed information about the stream

gitbox 2025-05-27

In PHP, stream is a very powerful abstract concept used to uniformly process various data sources and targets such as files, networks, and memory. When we manipulate streams, it is very important to understand their filters and metadata. This article will introduce how to use stream_get_filters() and stream_get_meta_data() in combination to get detailed information about the stream.

1?? What is stream_get_filters() ?

stream_get_filters() is a PHP built-in function that lists all current available stream filters. Stream filters can be used to modify data in streams, such as compression, encryption, encoding and conversion.

 <?php
$filters = stream_get_filters();
print_r($filters);
?>

Run this code and you may see output like this:

 Array
(
    [0] => string.rot13
    [1] => string.toupper
    [2] => zlib.inflate
    [3] => zlib.deflate
    ...
)

This tells us the filters supported in the current PHP configuration.

2?? What is stream_get_meta_data() ?

stream_get_meta_data() is used to obtain meta information of an open stream. It returns an associative array containing various details, such as:

  • Is the stream readable or writeable?

  • Whether the end of the file (eof) is reached

  • Encapsulation protocol (such as http , file )

  • Whether it is blocked

  • Timeout setting

Example:

 <?php
$fp = fopen('http://gitbox.net/', 'r');
$meta = stream_get_meta_data($fp);
print_r($meta);
fclose($fp);
?>

The output is similar:

 Array
(
    [wrapper_type] => http
    [stream_type] => tcp_socket/ssl
    [mode] => r
    [unread_bytes] => 0
    [seekable] =>
    [timed_out] =>
    [blocked] => 1
    [eof] =>
)

3?? Use in combination: Get detailed information about the flow

We can combine the two, first get the supported filter, then open a stream, view its meta-information, and try to attach a filter to it.

Sample code:

 <?php
// List currently available filters
$filters = stream_get_filters();
echo "List of available filters:\n";
foreach ($filters as $filter) {
    echo "- $filter\n";
}

// Open a HTTP flow
$url = 'http://gitbox.net/';
$fp = fopen($url, 'r');

if (!$fp) {
    die("Unable to open $url\n");
}

// 查看flow的元数据
$meta = stream_get_meta_data($fp);
echo "\nflow的元信息:\n";
print_r($meta);

// Try attaching a filter(If supported zlib.inflate)
if (in_array('zlib.inflate', $filters)) {
    stream_filter_append($fp, 'zlib.inflate', STREAM_FILTER_READ);
    echo "Attached zlib.inflate 过滤器到flow。\n";
} else {
    echo "zlib.inflate Filter not available。\n";
}

fclose($fp);
?>

4?? Notes

  • Not all streams support all filters. For example, http streams may not support zlib.inflate and need to be judged based on the context.

  • Before using stream_filter_append(), it is best to use stream_get_filters() to check whether the filter exists.

  • stream_get_meta_data() returns only the current state per call, and the stream state may change with read or write.