PHP stream_filter_append() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_filter_append() attaches a named filter to the end of a stream's filter chain so data is transformed as it is read or written.

Built-in filters such as string.toupper and string.rot13 are enough for many beginner demos. Because the filter is appended, it runs after filters that were already attached.

Syntax

Call stream_filter_append() with the values you need. Start with this line.

PHP
$filter = stream_filter_append($stream, "string.toupper");
CallWhat it does
stream_filter_append($fp, "string.toupper")Appends the uppercase filter using the stream's default read/write mode.
stream_filter_append($fp, "string.toupper", STREAM_FILTER_WRITE)Applies the filter only when writing.
stream_filter_append($fp, "string.rot13", STREAM_FILTER_READ)Applies ROT13 only when reading.

Appended filters also process buffered data already waiting on the stream. That differs from stream_filter_prepend(). Remove a filter later with stream_filter_remove() using the resource this function returns.

Library declarations
PHP
stream_filter_append(resource $stream, string $filter_name, int $mode = 0, mixed $params = null): resource|false

Parameters

ParameterDescription
streamRequired. The stream resource to filter.
filter_nameRequired. Built-in or registered filter name, for example string.toupper.
modeOptional. STREAM_FILTER_READ, STREAM_FILTER_WRITE, or STREAM_FILTER_ALL. Default follows the stream open mode.
paramsOptional. Extra parameters for filters that accept them.

Return Value

Return ValueDescription
resource|falseA filter resource on success (usable with stream_filter_remove()), or false on failure.

PHP Examples

Example 1 — Uppercase Writes With string.toupper

Append string.toupper on a memory stream and write lowercase text.

PHP
<?php
$fp = fopen("php://memory", "w+");
stream_filter_append($fp, "string.toupper", STREAM_FILTER_WRITE);

fwrite($fp, "hello filter");
rewind($fp);
echo stream_get_contents($fp), PHP_EOL;
fclose($fp);

How It Works

  1. php://memory is opened for reading and writing.
  2. string. toupper is appended on the write chain.
  3. fwrite() sends lowercase text through the filter.
  4. After rewind, the stored text is HELLO FILTER.

Example 2 — Apply ROT13 Only on Write

Use STREAM_FILTER_WRITE so reading the raw stored bytes shows the transformed text.

PHP
<?php
$fp = fopen("php://memory", "w+");
stream_filter_append($fp, "string.rot13", STREAM_FILTER_WRITE);

fwrite($fp, "This is a test");
rewind($fp);
echo stream_get_contents($fp), PHP_EOL;
fclose($fp);

How It Works

  1. ROT13 is attached only for writes.
  2. The plain sentence is written through the filter.
  3. The memory stream stores the transformed bytes.
  4. Reading them back prints Guvf vf n grfg.

Example 3 — Append Two Filters in Order

Append string.tolower first, then string.toupper, and watch the final write result.

PHP
<?php
$fp = fopen("php://memory", "w+");
stream_filter_append($fp, "string.tolower", STREAM_FILTER_WRITE);
stream_filter_append($fp, "string.toupper", STREAM_FILTER_WRITE);

fwrite($fp, "MiXeD");
rewind($fp);
echo stream_get_contents($fp), PHP_EOL;
fclose($fp);

How It Works

  1. tolower is appended first, toupper second.
  2. On write, earlier filters run before later ones in the chain.
  3. The final filter leaves the text uppercase.
  4. Reading the stream shows MIXED.

Frequently Asked Questions

It attaches a filter to the end of a stream's filter list so data is transformed during read or write operations.
Append adds the filter at the end of the chain. Prepend adds it at the beginning, so it runs earlier.
string.toupper, string.tolower, and string.rot13 are simple built-in demos that work well with php://memory.
It identifies that filter instance so you can remove it later with stream_filter_remove().
Filters on STDOUT do not affect echo/print. Write with fwrite() to the filtered stream instead.

Next: stream_filter_prepend()

stream_filter_prepend() attaches a named filter at the beginning of a stream's filter chai.

stream_filter_prepend() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful