PHP Stream: stream_set_read_buffer and stream_set_write_buffer

First something about stream from PHP.net:
Stream came to PHP in version 4.3 to generalize file, network, data compression, and other operations which share a common set of functions and uses. In its simplest definition, a stream is a resource object which exhibits streamable behavior. That is, it can be read from or written to in a linear fashion, and may be able to fseek() to an arbitrary locations within the stream.

Now, the use of two functions: stream_set_read_buffer() and stream_set_write_buffer(). It is helpful in handling the buffer used in file read and write. For file write (fwrite()), 8 kilobytes of data is buffered by default. So, other process is waiting for write then that process will till 8K data is being written by the process taken handle of the resource. For a very small size of file write or read operation, this 8K data size can be large and unnecessary to keep other process waiting. In that case we can use the stream buffer functions to manipulate the file buffer. PHP 5 has added many more Stream function to streamline the streaming process.

If we want to complete the read process (fread) before any processes can try writing to the file then we can use the stream_set_read_buffer().

int stream_set_read_buffer ( resource $stream , int $buffer )

Buffer = 0 means read operation in unbuffered and ensures that all reads with fread() are completed before other processes are allowed to write to that output stream.

  • # 1 - by Navsingh Financial Technology

    Bug #45738 php stream error?
    i have an error when loading csv widget on my site.
    -partial widget
    -navbar not show

    Error state.
    Warning: fopen(/path_to/uploads/content.csv) [function.fopen]: failed to open stream: No such file or directory in /mnt/w0500/d41/s13/b02e8f66/www/lnaelite.org/widget/libs/Content.php on line 6

    Warning: fgetcsv(): supplied argument is not a valid stream resource in /mnt/w0500/d41/s13/b02e8f66/www/lnaelite.org/widget/libs/Content.php on line 7

    Warning: fclose(): supplied argument is not a valid stream resource in /mnt/w0500/d41/s13/b02e8f66/www/lnaelite.org/widget/libs/Content.php on line 18

Comments are open for an year period. Please, write here on Facebook page.