PHP Function Reference

PHP set_file_buffer() Function



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

Syntax

set_file_buffer(stream, buffer)

Parameters

stream Required. Specify the file pointer to truncate. The stream must be open for writing.
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 if the request is failed.

Example:

The example below demonstrates how to use this function to create an unbuffered stream.

<?php
$file = "test.txt";

$fp = fopen($file, "w");
if($fp){
  if(set_file_buffer($fp, 0) !== 0) {
    echo "Changing the buffering failed";
  } else {
    echo "Changing the buffering successful.";
  }
  
  fwrite($fp, "Hello World. Testing!");
  fclose($fp);
}
?>

The output of the above code will be:

Changing the buffering successful.

❮ PHP Filesystem Reference