Current Location: Home> Latest Articles> How to use stream_get_filters and stream_filter_append in combination

How to use stream_get_filters and stream_filter_append in combination

gitbox 2025-05-28

In PHP, Stream is a key concept for handling files, network connections, or other input/output resources. Stream filters are some functional modules attached to the stream that can process data in the stream and modify the data before it is read or written. stream_get_filters and stream_filter_append are two important functions in PHP that handle stream filters.

This article will explain in detail how to use these two functions and how they can be used together to implement filtering of streaming data.

What is stream_get_filters ?

The stream_get_filters function is used to get all stream filters currently available. Stream filters are predefined functions that can change the behavior of a stream or process data in the stream. You can view the registered stream filters in the system by calling stream_get_filters .

Sample code:

 $filters = stream_get_filters();
print_r($filters);

This code will output all available stream filters. For example, you may see filters such as string.toupper , convert.base64-encode , etc. Each filter corresponds to a specific data conversion or operation.

What is stream_filter_append ?

The stream_filter_append function is used to append a filter to a stream. Unlike stream_filter_prepend , stream_filter_append appends a filter at the end of the stream, so it processes the data before the stream's output data is returned. You can use stream_filter_append to attach a custom stream filter, or use the system-provided filter.

Sample code:

 $fp = fopen("php://temp", "r+");
stream_filter_append($fp, "string.toupper");

fwrite($fp, "hello world\n");
rewind($fp);
echo fread($fp, 1024); // Output: HELLO WORLD
fclose($fp);

In the example above, we use stream_filter_append to append the string.toupper filter to the file stream so that the written content is automatically converted to uppercase letters.

How to use stream_get_filters and stream_filter_append in combination?

By combining these two functions, you can first look at the available filters and select a suitable filter to apply to your stream. First, you can use stream_get_filters to view all filters, and then attach a filter to the stream through stream_filter_append , thereby changing the way the stream data is processed.

Example: Use in conjunction

Suppose we want to read the contents of a file and convert all text into capital letters. We will use stream_get_filters to view available filters, select a suitable filter, and then use stream_filter_append to filter the file stream.

 // View available filters
$filters = stream_get_filters();
print_r($filters);

// Choose the right filter(string.toupper)and attached to the stream
$fp = fopen("sample.txt", "r+");
stream_filter_append($fp, "string.toupper");

// Write and read file contents
fwrite($fp, "hello world\n");
rewind($fp);
echo fread($fp, 1024); // Output: HELLO WORLD
fclose($fp);

Use custom filters

In addition to the system-provided filters, PHP also allows users to create custom stream filters. To use custom filters, you can use stream_filter_append to attach these filters. Here is a simple custom filter example that demonstrates how to convert all lowercase letters in text to uppercase letters.

Custom filter code example:

 // Create a custom filter
class UppercaseFilter extends php_user_filter {
    public function filter($in, $out) {
        while ($bucket = stream_bucket_make_writeable($in)) {
            // Convert to capital
            $bucket->data = strtoupper($bucket->data);
            stream_bucket_append($out, $bucket);
        }
        return PSFS_PASS_ON;
    }
}

// Register filter
stream_filter_register("uppercase", "UppercaseFilter");

// Use custom filters
$fp = fopen("php://temp", "r+");
stream_filter_append($fp, "uppercase");

fwrite($fp, "hello world\n");
rewind($fp);
echo fread($fp, 1024); // Output: HELLO WORLD
fclose($fp);

In this example, we create a custom stream filter, UppercaseFilter , which converts all lowercase letters in the input stream to uppercase letters.

Conclusion

stream_get_filters and stream_filter_append are the core functions of the powerful stream filter mechanism in PHP. With these functions, you can easily manage the filters of the stream and process the data in the stream in real time. Using these functions in combination can help you flexibly process various data streams and meet the needs of different scenarios.

In practice, you can achieve more complex data processing needs by looking at available stream filters, selecting the most suitable filter, or by customizing the filters.

Hope this article will be helpful for you to understand and use the stream_get_filters and stream_filter_append functions!