PHP stream_set_write_buffer() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_set_write_buffer() sets the size of the write buffer for an open stream.

A larger buffer can group small fwrite() calls before they reach the underlying file or socket. Pass 0 for unbuffered writes so each write goes out right away. The function returns 0 on success.

Syntax

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

PHP
$result = stream_set_write_buffer($stream, 0);
CallWhat it does
stream_set_write_buffer($stream, 0)Turns off write buffering so each write is sent immediately.
stream_set_write_buffer($stream, 8192)Requests an 8 KB write buffer.
stream_set_write_buffer($stream, 4096)Requests a 4 KB write buffer.

Return value 0 means success. Any other value means the request failed. Pair with fflush() when you need data flushed before closing. stream_set_read_buffer() is the matching control for reads.

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

Parameters

ParameterDescription
streamRequired. An open stream resource, such as a file handle from fopen().
sizeRequired. Desired buffer size in bytes. Use 0 for unbuffered writes.

Return Value

Return ValueDescription
0The buffer size was applied successfully.
intA non-zero value means the buffer could not be set as requested.

PHP Examples

Example 1 — Disable Write Buffering

Open a temp file, turn off the write buffer, and print the result code.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_write_buf.txt';
$stream = fopen($path, 'w');

$result = stream_set_write_buffer($stream, 0);
echo 'result=', $result, PHP_EOL;

fwrite($stream, "line one\n");
fclose($stream);
unlink($path);

How It Works

  1. fopen() opens a real file stream for writing.
  2. stream_set_write_buffer($stream, 0) requests unbuffered writes.
  3. A return of 0 means success.
  4. fwrite() then goes out without waiting for a full buffer.

Example 2 — Request an 8 KB Buffer

Ask for an 8192-byte write buffer and confirm the success code.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_write_buf2.txt';
$stream = fopen($path, 'w');

$result = stream_set_write_buffer($stream, 8192);
echo $result === 0 ? '8KB buffer ok' : 'buffer failed', PHP_EOL;

fclose($stream);
unlink($path);

How It Works

  1. 8192 bytes is a common buffer size for file writes.
  2. Compare the return value with === 0.
  3. Success means PHP accepted the request for this stream.
  4. Clean up the demo file with unlink().

Example 3 — Flush After Buffered Writes

Keep a buffer, write data, then fflush() so the bytes leave PHP before fclose().

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_write_buf3.txt';
$stream = fopen($path, 'w');
stream_set_write_buffer($stream, 4096);

fwrite($stream, 'hello');
$flushed = fflush($stream);
echo 'flushed=', $flushed ? 'yes' : 'no', PHP_EOL;

fclose($stream);
unlink($path);

How It Works

  1. A 4096-byte buffer may hold small writes temporarily.
  2. fflush() pushes pending bytes to the file.
  3. true means the flush succeeded.
  4. Always fflush() or fclose() when another reader needs the data now.

Frequently Asked Questions

It sets how many bytes PHP may gather before sending writes to the underlying file or socket. Size 0 means unbuffered.
0 means success. Any other integer means the buffer size could not be applied.
Use 0 when another process must see each write right away, such as a log file that a second script tails.
No. File streams usually support it. Some sockets, filters, or remote wrappers may ignore the request.
stream_set_write_buffer() tunes outgoing writes. stream_set_read_buffer() tunes how PHP buffers incoming reads.

Next: stream_socket_accept()

stream_socket_accept() accepts an incoming connection on a listening server socket created.

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