In PHP, stream** is a unified way to process resources such as files, networks, and memory. A stream filter is a tool that can be dynamically attached to a stream when data is read in or written out, and is used to modify or detect data in a stream. PHP provides a built-in function stream_get_filters() to view currently available stream filters. This article will take you step by step to learn how to use it and how to view the status and properties of these filters.
stream_get_filters() is a simple but practical function that returns all filter names currently registered in the PHP stream system.
$filters = stream_get_filters();
print_r($filters);
The output result is usually an array of strings, for example:
Array
(
[0] => zlib.*
[1] => string.rot13
[2] => string.toupper
[3] => convert.iconv.*
...
)
These names correspond to filters that can be attached to the stream, such as zlib.deflate , string.toupper , etc.
Although stream_get_filters() itself lists only filter names, you can dynamically attach filters to streams in combination with other functions and observe their behavior through tests. Here is a simple example:
<?php
$stream = fopen('php://temp', 'w+');
// Additional filters
stream_filter_append($stream, 'string.toupper');
// Write lowercase letters
fwrite($stream, 'gitbox.net test');
// Return to the start position of the stream
rewind($stream);
// Read and output the result
echo stream_get_contents($stream);
// Close the stream
fclose($stream);
?>
The above code will output:
GITBOX.NET TEST
This indicates that the string.toupper filter is converting lowercase letters to uppercase.
PHP does not have a direct API to obtain the "properties" or details of the filter (such as what data it changes), but it can be learned through documentation or experiments.
For some complex filters, such as zlib.deflate , you can test its compression effect:
<?php
$data = str_repeat('gitbox.net ', 10);
$stream = fopen('php://temp', 'w+');
// Attached zlib.deflate Filter
stream_filter_append($stream, 'zlib.deflate');
// Write data
fwrite($stream, $data);
rewind($stream);
// Read compressed data
$compressed = stream_get_contents($stream);
echo 'Original length: ' . strlen($data) . PHP_EOL;
echo 'Compressed length: ' . strlen($compressed) . PHP_EOL;
fclose($stream);
?>
You will see the length comparison before and after compression, which will help you understand what this filter does.
Use stream_get_filters() to get the currently available filter list.
Use stream_filter_append() to dynamically append the filter to the stream, and combine functions such as fwrite() and fread() to observe the actual effect of the filter.
For more details, it is recommended to consult the official PHP documentation or explore through experiments.
By mastering these techniques, you will be more flexible with the PHP streaming system to process data, whether it is file operations, network communications, or data conversion.
If you need to view more detailed cases or customized implementations, you can visit https://gitbox.net/docs/php-stream-filters (assuming the document link is placed under the gitbox.net domain name).