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.