W3docs

set_file_buffer()

Learn how PHP's stream_set_write_buffer() controls write buffering on a file stream, why set_file_buffer() is deprecated, and how to disable buffering.

Introduction

In PHP, the stream_set_write_buffer() function is used to set the write buffering size of a file stream. It is a useful function for optimizing the performance of your PHP scripts by controlling how data is flushed to disk. Note that the older set_file_buffer() function was deprecated in PHP 4.3.0 and removed in PHP 5.0.0, so stream_set_write_buffer() is the correct modern approach. In this article, we will cover everything you need to know about the stream_set_write_buffer() function, including its syntax, parameters, and examples of how it can be used.

Understanding the stream_set_write_buffer() Function

The stream_set_write_buffer() function in PHP is used to set the write buffering size of a stream. It takes two parameters: the stream resource and the buffer size in bytes.

When you use stream_set_write_buffer(), PHP configures the stream to buffer up to the specified number of bytes before flushing data to disk. Setting the buffer size to 0 disables buffering entirely, which is a common configuration for real-time output. The buffer is automatically flushed when it reaches the specified size, when the script ends, or when you explicitly call fflush() on the stream. Setting this appropriately can be useful for optimizing the performance of your PHP scripts, especially when writing large amounts of data.

Syntax of the stream_set_write_buffer() Function

The syntax of the stream_set_write_buffer() function is as follows:

stream_set_write_buffer($stream, $buffer);

The parameters are:

  • $stream — a writable stream resource, typically returned by fopen().
  • $buffer — the buffer size in bytes. Use 0 to disable buffering so every write is flushed immediately.

According to the PHP manual the function returns 0 on success. In practice the return value is platform-dependent (some systems return a non-zero value even when buffering is configured), so you should not branch on it. If you need a guaranteed flush, call fflush() instead of relying on the return code.

Examples of Using stream_set_write_buffer()

Let's take a look at an example of how the stream_set_write_buffer() function can be used in PHP.

Example 1: Setting the File Buffering

<?php

$file_handle = fopen('example.txt', 'w');
stream_set_write_buffer($file_handle, 1024);
fwrite($file_handle, 'Hello, world!');
fclose($file_handle);

This example opens the example.txt file for writing and sets the write buffer to 1024 bytes. PHP now holds written data in memory until 1024 bytes accumulate, until you call fflush(), or until the stream is closed with fclose() — which flushes any remaining buffered data and frees system resources. Always close the handle when finished.

Example 2: Disabling Buffering for Immediate Writes

When you want each write to reach disk right away — for example, when writing a log file that another process tails in real time — set the buffer size to 0:

<?php

$log = fopen('app.log', 'a');
stream_set_write_buffer($log, 0); // unbuffered: every fwrite() flushes immediately

fwrite($log, "Request received\n");
fwrite($log, "Response sent\n");

fclose($log);

With buffering disabled, each fwrite() call is written through immediately instead of waiting for the buffer to fill, at the cost of more frequent disk I/O.

Conclusion

The stream_set_write_buffer() function in PHP lets you optimize script performance by controlling how much data a stream buffers before flushing it to disk — and disabling buffering entirely with a size of 0 when you need immediate writes. Because its return value is not reliable across platforms, pair it with fflush() when you must force a flush. To go further, explore fopen() for opening streams, fwrite() for writing to them, and fclose() for closing them cleanly.

Practice

Practice
What is the correct, modern way to set the write buffer of a file stream in PHP?
What is the correct, modern way to set the write buffer of a file stream in PHP?
Was this page helpful?