PHP Function Reference

PHP stream_set_read_buffer() Function



The PHP stream_set_read_buffer() function sets read file buffering on the given stream. It is the equivalent of stream_set_write_buffer(), but for read operations.

Syntax

stream_set_read_buffer(stream, size)

Parameters

stream Required. Specify the file pointer.
size Required. Specify the number of bytes to buffer. If size is 0 then read operations are unbuffered. This ensures that all reads with fread() are completed before other processes are allowed to read from that input stream.

Return Value

Returns 0 on success, or another value on failure.

Example:

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

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

$fp = fopen($file, "w");
fwrite($fp, $message);
fclose($fp);

$fp = fopen($file, "r");
if ($fp) {
  if (stream_set_read_buffer($fp, 0) !== 0) {
    echo "Changing the buffering failed \n";
  }

  //reading content of the file
  $content = fread($fp, filesize("test.txt"));

  //displaying the read content
  echo $content;

  //close the file
  fclose($fp);
}
?>

The output of the above code will be:

Hello World!

❮ PHP Streams Reference