stream_set_write_buffer
(PHP 4 >= 4.3.0, PHP 5)
stream_set_write_buffer — Sets write file buffering on the given stream
Description
$stream
, int $buffer
)
Sets the buffering for write operations on the given stream
to buffer
bytes.
Output using fwrite() is normally buffered at 8K. This means that if there are two processes wanting to write to the same output stream (a file), each is paused after 8K of data to allow the other to write.
Parameters
-
stream
-
The file pointer.
-
buffer
-
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 Values
Returns 0 on success, or EOF if the request cannot be honored.
Examples
Example #1 stream_set_write_buffer() example
The following example demonstrates how to use stream_set_write_buffer() to create an unbuffered stream.
<?php
$fp = fopen($file, "w");
if ($fp) {
stream_set_write_buffer($fp, 0);
fwrite($fp, $output);
fclose($fp);
}
?>
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Другие базовые расширения
- Потоки
- set_socket_blocking
- stream_bucket_append
- stream_bucket_make_writeable
- stream_bucket_new
- stream_bucket_prepend
- stream_context_create
- stream_context_get_default
- stream_context_get_options
- stream_context_get_params
- stream_context_set_default
- stream_context_set_option
- stream_context_set_params
- stream_copy_to_stream
- stream_encoding
- stream_filter_append
- stream_filter_prepend
- stream_filter_register
- stream_filter_remove
- stream_get_contents
- stream_get_filters
- stream_get_line
- stream_get_meta_data
- stream_get_transports
- stream_get_wrappers
- stream_is_local
- stream_notification_callback
- stream_register_wrapper
- stream_resolve_include_path
- stream_select
- stream_set_blocking
- stream_set_chunk_size
- stream_set_read_buffer
- stream_set_timeout
- stream_set_write_buffer
- stream_socket_accept
- stream_socket_client
- stream_socket_enable_crypto
- stream_socket_get_name
- stream_socket_pair
- stream_socket_recvfrom
- stream_socket_sendto
- stream_socket_server
- stream_socket_shutdown
- stream_supports_lock
- stream_wrapper_register
- stream_wrapper_restore
- stream_wrapper_unregister
Коментарии
Trying to use stream_set_write_buffer on a local file will always fail according to this note from 2003.
http://grokbase.com/t/php/php-internals/0351gp6xtn/stream-set-write-buffer-does-not-work
Example:
$fp = fopen("localfile.txt", "w");
if (stream_set_write_buffer($fp, 0) !== 0) {
// changing the buffering failed is always true on local files
}
The stream_set_write_buffer() function has been broken since PHP 4.3.0. As a workaround, I would suggest using a php_user_filter as an output buffer. By using the following class I have measured over 50% performance improvements with scripts that generate large text files by writing them one line at a time:
class my_output_buffer extends php_user_filter
{
protected static $BUFFER_SIZE = 4096;
function onCreate( ) {
$this->bff = [];
$this->len = 0;
return true;
}
public function filter($in, $out, &$consumed, $closing)
{
$rv = PSFS_FEED_ME; /* assume no output */
/* process input */
while ($bucket = stream_bucket_make_writeable($in)) {
/* bucket is too big for the buffer? */
$space = static::$BUFFER_SIZE - $this->len;
if ($bucket->datalen >= $space) {
/* consume data by placing it into internal buffers */
$this->bff[] = substr($bucket->data, 0, $space);
$this->len += $space;
$overflow = substr($bucket->data, $space);
$ovfl_len = $bucket->datalen - $space;
$consumed += $bucket->datalen;
assert($this->len == static::$BUFFER_SIZE);
/* make one big bucket */
$bucket->data = implode('', $this->bff);
$bucket->datalen = static::$BUFFER_SIZE;
stream_bucket_append($out, $bucket);
$rv = PSFS_PASS_ON; /* we have output! */
/* handle overflow */
$this->bff = [$overflow];
$this->len = $ovfl_len;
}
else {
/* consume data by placing it into internal buffers */
$this->bff[] = $bucket->data;
$this->len += $bucket->datalen;
$consumed += $bucket->datalen;
}
}
/* stream is closing and we have data? */
if ($closing && $this->len > 0) {
/* make one last bucket */
$data = implode('', $this->bff);
$bucket = stream_bucket_new($this->stream, $data);
stream_bucket_append($out, $bucket);
$rv = PSFS_PASS_ON; /* we have output! */
/* clear internal buffer */
$this->bff = [];
$this->len = 0;
}
return $rv;
}
}
$fp = fopen('foobar.txt', 'w');
/* enable filtering */
stream_filter_register('output.buffer', 'my_output_buffer');
stream_filter_append($fp, 'output.buffer');
/* a lot of small writes */
for ($i = 0; $i < 10000; $i++) {
fwrite($fp, 'x');
}
fclose($fp);