Home PHP Stream stream_bucket_make_writeable() PHP stream_bucket_make_writeable() Function Definition In PHP, stream_bucket_make_writeable() takes the next bucket out of a brigade so your custom stream filter can read and change its data.
It is almost always called in a while loop inside php_user_filter::filter(). When no buckets remain, it returns null and the loop stops. This is advanced filter plumbing, not an everyday string function.
Syntax Call stream_bucket_make_writeable() with the values you need. Start with this line.
while ($bucket = stream_bucket_make_writeable($in)) { /* ... */ } Call What it does stream_bucket_make_writeable($in)Pulls the next writable bucket from the input brigade, or null when empty. $bucket->data = strtoupper($bucket->data)Typical next step: change the text stored in the bucket. $consumed += $bucket->datalenTell PHP how many bytes you consumed from that bucket.
The spelling is make_writeable (with an e). After PHP 8.4 the return type is StreamBucket; older versions returned a plain object with data and datalen properties.
Library declarations stream_bucket_make_writeable(resource $brigade): ?StreamBucket Parameters Parameter Description brigade Required. The input brigade resource, usually the $in argument of your filter() method.
Return Value Return Value Description StreamBucket|null The next bucket to edit, or null when the brigade has no more buckets.
PHP Examples 1. Read Buckets and Uppercase Them 2. Stop When the Brigade Is Empty 3. Replace Digits While Reading Buckets Example 1 — Read Buckets and Uppercase Them Pull each bucket, change its data, then send it out. php://memory keeps the demo offline.
<?php
class MakeWriteableUpperFilter extends php_user_filter
{
public function filter($in, $out, &$consumed, $closing): int
{
while ($bucket = stream_bucket_make_writeable($in)) {
$bucket->data = strtoupper($bucket->data);
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
}
stream_filter_register("demo.mw.upper", MakeWriteableUpperFilter::class);
$fp = fopen("php://memory", "w+");
stream_filter_append($fp, "demo.mw.upper", STREAM_FILTER_WRITE);
fwrite($fp, "make writeable");
rewind($fp);
echo stream_get_contents($fp), PHP_EOL;
fclose($fp); How It Works stream_bucket_make_writeable($in) returns one bucket at a time. We replace $bucket->data with an uppercase copy. stream_bucket_append() returns the bucket to the stream pipeline. Reading the memory stream prints MAKE WRITEABLE. Example 2 — Stop When the Brigade Is Empty When make_writeable() returns null, the while loop ends. An empty write still works safely.
<?php
class CountBucketsFilter extends php_user_filter
{
public function filter($in, $out, &$consumed, $closing): int
{
$count = 0;
while ($bucket = stream_bucket_make_writeable($in)) {
$count++;
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
if ($closing) {
stream_bucket_append($out, stream_bucket_new($this->stream, " buckets=" . $count));
}
return PSFS_PASS_ON;
}
}
stream_filter_register("demo.mw.count", CountBucketsFilter::class);
$fp = fopen("php://memory", "w+");
stream_filter_append($fp, "demo.mw.count", STREAM_FILTER_WRITE);
fwrite($fp, "abc");
rewind($fp);
echo stream_get_contents($fp), PHP_EOL;
fclose($fp); How It Works The loop runs once for the single data bucket from fwrite(). When no buckets remain, make_writeable() returns null and the loop stops. On close we append a short note that shows how many buckets we saw. The sample output is abc buckets=1. Frequently Asked Questions What does stream_bucket_make_writeable() do in PHP? It removes the next bucket from a brigade and returns it so your custom filter can change the data. When the brigade is empty it returns null.
Why is it spelled writeable? That is the historical PHP function name. Use stream_bucket_make_writeable exactly as written.
What properties does the bucket have? You mainly use data (the string chunk) and datalen (its length). In PHP 8.4+ the object is a StreamBucket instance.
Can beginners skip this function? Yes for most apps. Prefer built-in filters like string.toupper. Learn make_writeable only when you need a custom filter class.
Where does the $in brigade come from? PHP passes it into your filter() method when data flows through a stream that has your filter attached.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion