Function name: stream_filter_append()
Applicable version: PHP 4 >= 4.3.0, PHP 5, PHP 7
Function description: Attach a filter to the specified stream
Syntax: bool stream_filter_append ( resource $stream , string $filtername [, int $read_write [, mixed $params ]] )
parameter:
Return value: Return true when successful, false when failure
Example:
<?php // 创建一个文件流$stream = fopen('data.txt', 'r'); // 附加一个过滤器到流上if (stream_filter_append($stream, 'convert.base64-encode')) { // 读取并输出流的内容while (!feof($stream)) { echo fread($stream, 4096); } // 关闭流fclose($stream); } else { echo "无法附加过滤器到流上"; } ?>
In the above example, we first create a file stream using the fopen()
function, and then attach the convert.base64-encode
filter to the stream using the stream_filter_append()
function. Next, we use the fread()
function to read the contents of the stream and use echo
output. Finally, we use the fclose()
function to close the stream.
Note that convert.base64-encode
is a built-in filter that converts data in a stream to Base64 encoding. Of course, you can also use other filters or custom filters to implement different functions.