#46171 [Opn]: stream_bucket_new don't work with write streams
| From: | lbarnaud@php.net | Date: | Sun, 05 Oct 2008 21:19:54 +0000 |
| Subject: | #46171 [Opn]: stream_bucket_new don't work with write streams | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-129490@lists.php.net to get a copy of this message | ||
ID: 46171
Updated by: lbarnaud@php.net
Reported By: Keisial at gmail dot com
Status: Open
Bug Type: Streams related
Operating System: *
PHP Version: 5.3CVS-2008-09-25 (snap)
New Comment:
The problem is that the stream is partially closed (resource deleted)
when filter() is called with $closing==true. That is, $this->stream is
not a valid resource at this point. You may want to fflush() the stream
before closing, and not use ->stream when closing.
Don't know if it's a bug or documentation problem (actually, not making
the stream resource available during fclose() is not that bad).
Previous Comments:
------------------------------------------------------------------------
[2008-09-25 10:35:21] Keisial at gmail dot com
Description:
------------
Calling stream_bucket_new on a write stream doesn't work (the same code
fopening for reading works).
Reproduce code:
---------------
<?php /** Adapted from
http://php.net/manual/function.stream-filter-register.php
**/
/* Define our filter class */
class strtoupper_filter extends php_user_filter {
function filter($in, $out, &$consumed, $closing)
{
$new_bucket = stream_bucket_new($this->stream, "Uppercase text: ");
if ($new_bucket === false) throw new Exception("stream_bucket_new
should have returned a bucket");
stream_bucket_append($out, $new_bucket);
while ($bucket = stream_bucket_make_writeable($in)) {
$bucket->data = strtoupper($bucket->data);
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
}
/* Register our filter with PHP */
stream_filter_register("strtoupper", "strtoupper_filter")
or die("Failed to register filter");
$fp = fopen("foo-bar.txt", "w");
/* Attach the registered filter to the stream just opened */
stream_filter_append($fp, "strtoupper");
fwrite($fp, "Line1\n");
fwrite($fp, "Word - 2\n");
fwrite($fp, "Easy As 123\n");
fclose($fp);
// Read the contents back out
readfile("foo-bar.txt");
?>
Actual result:
--------------
Warning: stream_bucket_new(): 5 is not a valid stream resource in
bucket_new_testcase.php on line 7
Fatal error: Uncaught exception 'Exception' with message
'stream_bucket_new should have returned a bucket'
Stack trace:
#0 [internal function]: strtoupper_filter->filter(Resource id #20,
Resource id #21, 0, true)
#1 bucket_new_testcase.php(36): fclose(Resource id #5)
#2 {main}
thrown in bucket_new_testcase.php on line 8
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=46171&edit=1