PHP set_file_buffer() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, set_file_buffer() sets the write buffer size for a file stream. It is an alias of stream_set_write_buffer(), so both names do exactly the same thing.

A write buffer collects data in memory before it is sent to the file. A size of 0 means no buffering, so each write goes straight to the file. Bigger sizes can make many small writes faster.

Syntax

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

PHP
$result = set_file_buffer($handle, 0);
CallWhat it does
set_file_buffer($handle, 0)Turns write buffering off so data is written right away.
set_file_buffer($handle, 8192)Asks PHP to use a buffer of 8192 bytes for writing.
stream_set_write_buffer($handle, 0)The modern name for the same function. Use it in new code.

set_file_buffer() is an alias of stream_set_write_buffer(). New code should use stream_set_write_buffer() because that name is clearer, but the old name still works. It only affects writing. To change read buffering, use stream_set_read_buffer().

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

Parameters

ParameterDescription
streamRequired. An open stream returned by fopen() or a similar function.
sizeRequired. The number of bytes to use for the write buffer. Use 0 for no buffering.

Return Value

Return ValueDescription
intReturns 0 on success, or another value (such as -1) if the request cannot be honored.

PHP Examples

Example 1 — Turn Off Write Buffering

Open a file for writing and tell PHP not to buffer the output.

PHP
<?php
$file   = sys_get_temp_dir() . '/buffer_demo.txt';
$handle = fopen($file, 'w');

$result = set_file_buffer($handle, 0);

echo 'Result: ', $result, PHP_EOL;

fclose($handle);
unlink($file);

How It Works

  1. fopen() opens a new file for writing and gives us a stream handle.
  2. set_file_buffer() with size 0 asks PHP to write data immediately.
  3. The function returns 0 when the request is accepted, so the script prints Result: 0.
  4. The file is closed and removed to keep the example clean.

Example 2 — Check the Return Value Correctly

Success is 0, not true. Compare with === 0 and also see that the alias gives the same answer.

PHP
<?php
$file   = sys_get_temp_dir() . '/buffer_edge.txt';
$handle = fopen($file, 'w');

$old = set_file_buffer($handle, 4096);
$new = stream_set_write_buffer($handle, 4096);

echo $old === 0 ? 'Buffer set' : 'Buffer not changed', PHP_EOL;
echo $old === $new ? 'Both names returned the same value' : 'Different results', PHP_EOL;

fclose($handle);
unlink($file);

How It Works

  1. set_file_buffer() asks for a 4096 byte buffer on the open file.
  2. stream_set_write_buffer() is called with the same arguments to compare.
  3. The strict check === 0 tells us the first request worked.
  4. Because one function is an alias of the other, both results are equal.

Example 3 — See Data Written Right Away

With buffering off, the text reaches the file immediately, so filesize() can see it before fclose().

PHP
<?php
$file   = sys_get_temp_dir() . '/buffer_live.txt';
$handle = fopen($file, 'w');

set_file_buffer($handle, 0);
fwrite($handle, 'Hello');

clearstatcache();
echo 'Size before fclose: ', filesize($file), ' bytes', PHP_EOL;

fclose($handle);
unlink($file);

How It Works

  1. set_file_buffer() with 0 means fwrite() does not hold data in a buffer.
  2. fwrite() writes the five letters of Hello to the file.
  3. clearstatcache() makes sure PHP does not use an old cached file size.
  4. filesize() reports 5 bytes even though the file is still open. This is useful for log files you watch live.

Frequently Asked Questions

It sets the size of the write buffer for an open file stream. A size of 0 turns buffering off.
Yes. set_file_buffer() is an alias, so both behave the same. Use stream_set_write_buffer() in new code for a clearer name.
It returns 0 on success. Any other value, such as -1, means the buffer size could not be changed for that stream.
When you need data to appear in the file immediately, for example in a live log file. For normal scripts the default buffering is fine.
No. It only affects writing. Use stream_set_read_buffer() to control the read buffer.

Next: stat()

stat() returns detailed information about a file or folder, such as its size, permissions .

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