PHP stream_set_read_buffer() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_set_read_buffer() sets the read buffer size for a stream, similar to stream_set_write_buffer() but for incoming data.

Pass 0 to request unbuffered reads. On success the function returns 0. If the request cannot be honored, it returns another value.

Syntax

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

PHP
$result = stream_set_read_buffer($stream, 0);
CallWhat it does
stream_set_read_buffer($stream, 0)Requests unbuffered reads; returns 0 on success.
stream_set_read_buffer($stream, 8192)Asks for an 8 KiB read buffer.
stream_set_read_buffer($stream, 4096)Asks for a 4 KiB read buffer.

Buffering behavior depends on the underlying stream. Success is reported as integer 0. Tutorial demos use php://memory / php://temp and print that success code.

Library declarations
PHP
stream_set_read_buffer(resource $stream, int $size): int

Parameters

ParameterDescription
streamRequired. The stream whose read buffer you want to set.
sizeRequired. Buffer size in bytes. 0 means unbuffered reads.

Return Value

Return ValueDescription
int0 on success; another value if the buffering request cannot be honored.

PHP Examples

Example 1 — Request Unbuffered Reads

Set the read buffer to 0 and print the success code.

PHP
<?php
$stream = fopen("php://memory", "r+");
$result = stream_set_read_buffer($stream, 0);
echo "result=", $result, PHP_EOL;
fclose($stream);

How It Works

  1. A memory stream is opened.
  2. stream_set_read_buffer($stream, 0) asks for unbuffered reads.
  3. A return value of 0 means the request succeeded.
  4. The script prints result=0.

Example 2 — Set a Positive Read Buffer

Ask for a 4096-byte buffer and confirm success the same way.

PHP
<?php
$stream = fopen("php://temp", "r+");
$result = stream_set_read_buffer($stream, 4096);
echo $result === 0 ? "ok" : "not honored", PHP_EOL;
fclose($stream);

How It Works

  1. php://temp is opened for the demo.
  2. A 4096-byte read buffer is requested.
  3. Comparing the return value with 0 shows success.
  4. The printed label is ok.

Example 3 — Tune Then Read Normally

Set buffering, write sample bytes, then read them back with stream_get_contents().

PHP
<?php
$stream = fopen("php://memory", "r+");
stream_set_read_buffer($stream, 0);

fwrite($stream, "buffer-demo");
rewind($stream);
echo stream_get_contents($stream), PHP_EOL;
fclose($stream);

How It Works

  1. Read buffering is set to unbuffered mode.
  2. A short string is written into the memory stream.
  3. After rewind(), stream_get_contents() returns the same text.
  4. Buffer tuning did not change the stored payload.

Frequently Asked Questions

It sets the read buffer size for a stream. Use 0 for unbuffered reads. It returns 0 when the request succeeds.
stream_set_read_buffer() tunes reading. stream_set_write_buffer() tunes writing. They are matching tools for opposite directions.
Success. It does not mean the stream is empty.
When you want each read to go to the underlying source without extra user-space buffering, for example when cooperating with other readers.
No. You still pass a length to fread(). The read buffer is a lower-level setting about how PHP buffers input.

Next: stream_set_timeout()

stream_set_timeout() sets how long a stream will wait during a read or write before giving.

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