#46171 [Tbd->Csd]: stream_bucket_new() does not work with write streams
| From: | lbarnaud@php.net | Date: | Sat, 28 Mar 2009 01:25:01 +0000 |
| Subject: | #46171 [Tbd->Csd]: stream_bucket_new() does not work with write streams | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-2059@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: To be documented
+Status: Closed
Bug Type: Documentation problem
Operating System: *
PHP Version: 5.3CVS-2008-09-25 (snap)
New Comment:
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation
better.
Previous Comments:
------------------------------------------------------------------------
[2009-03-27 02:56:18] lbarnaud@php.net
To be documented: The stream resource may not be available when the
filter() method is called with the $closing argument set to true.
------------------------------------------------------------------------
[2008-10-05 21:19:53] lbarnaud@php.net
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).
------------------------------------------------------------------------
[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