PHP Function Reference

PHP stream_set_write_buffer() Function

The PHP stream_set_write_buffer() function sets the buffering for write operations on the given stream to buffer bytes.


stream_set_write_buffer(stream, buffer)


stream Required. Specify the file pointer.
buffer Required. Specify the number of bytes to buffer. If buffer is 0 then write operations are unbuffered. This ensures that all writes with fwrite() are completed before other processes are allowed to write to that output stream.

Return Value

Returns 0 on success, or another value on failure.


The example below shows how to use stream_set_write_buffer() function to create an unbuffered stream.

$file = "test.txt";
$message = "Hello World!";

$fp = fopen($file, "w");
if ($fp) {
  if (stream_set_write_buffer($fp, 0) !== 0) {
    echo "Changing the buffering failed \n";
  fwrite($fp, $message);

//reading the content of the file
echo "File content: ", file_get_contents($file);

The output of the above code will be:

Changing the buffering failed 
File content: Hello World!

❮ PHP Streams Reference