PHP stream_filter_remove() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_filter_remove() takes a filter resource you got from stream_filter_append() or stream_filter_prepend() and detaches that filter from its stream.

Any data still sitting in the filter's internal buffer is flushed to the next stage before the filter goes away. After removal, later reads or writes skip that filter, so you can turn filtering on for part of a stream and off again.

Syntax

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

PHP
$ok = stream_filter_remove($filter);
CallWhat it does
stream_filter_remove($filter)Detaches the filter resource and returns true on success.
$f = stream_filter_append($fp, "string.rot13"); stream_filter_remove($f);Attaches rot13, then removes it so later writes are plain.
stream_filter_remove($bad)Returns false when the filter cannot be removed.

You must pass the filter resource itself, not the stream. Keep the return value of stream_filter_append() or stream_filter_prepend() in a variable so you can remove it later.

Library declarations
PHP
stream_filter_remove(resource $stream_filter): bool

Parameters

ParameterDescription
stream_filterRequired. The filter resource returned when you appended or prepended the filter.

Return Value

Return ValueDescription
booltrue when the filter was removed; false on failure.

PHP Examples

Example 1 — Remove a Write Filter Midway

Attach string.rot13 for the first write, remove it, then write again. Only the first part is encoded.

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

fwrite($fp, "This is ");
stream_filter_remove($filter);
fwrite($fp, "a test");

rewind($fp);
echo stream_get_contents($fp), PHP_EOL;
fclose($fp);

How It Works

  1. php://temp opens an in-memory stream that behaves like a temporary file.
  2. stream_filter_append() adds string. rot13 on the write side and returns a filter resource.
  3. The first fwrite() is rot13-encoded; stream_filter_remove() detaches the filter afterward.
  4. The second fwrite() is stored as plain text, so the combined result is "Guvf vf a test".

Example 2 — Confirm Success With a Boolean

Print whether stream_filter_remove() succeeded so you can see the return value in a small script.

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

$ok = stream_filter_remove($filter);
echo $ok ? "removed" : "failed", PHP_EOL;
fclose($fp);

How It Works

  1. A memory stream is opened and an uppercase write filter is attached.
  2. stream_filter_remove() is called with that filter resource.
  3. On success it returns true, so the script prints "removed".
  4. fclose() closes the stream when you are done.

Example 3 — Filter Only Part of a Message

Encode one word with rot13, remove the filter, then append a clear suffix.

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

fwrite($fp, "secret");
stream_filter_remove($filter);
fwrite($fp, "-ok");

rewind($fp);
echo stream_get_contents($fp), PHP_EOL;
fclose($fp);

How It Works

  1. "secret" is written while rot13 is active, becoming "frperg".
  2. stream_filter_remove() turns filtering off.
  3. "-ok" is written as plain characters.
  4. Rewinding and reading shows the mixed result in one string.

Frequently Asked Questions

It detaches a stream filter you previously attached with stream_filter_append() or stream_filter_prepend(). Remaining buffered filter data is flushed first.
Pass the filter resource returned by stream_filter_append() or stream_filter_prepend(), not the stream handle itself.
No. Bytes already written through the filter stay transformed. Only later I/O skips the removed filter.
PHP flushes the filter's internal buffer to the next stage before the filter is removed, so pending output is not lost.
Yes. Call stream_filter_append() or stream_filter_prepend() again when you want filtering back on that stream.

Next: stream_get_contents()

stream_get_contents() reads the remaining data from an open stream and returns it as a str.

stream_get_contents() 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