php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #46171 stream_bucket_new() does not work with write streams
Submitted: 2008-09-25 10:35 UTC Modified: 2009-03-28 01:25 UTC
From: Keisial at gmail dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.3CVS-2008-09-25 (snap) OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: Keisial at gmail dot com
New email:
PHP Version: OS:

 

 [2008-09-25 10:35 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-05 21:19 UTC] 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).
 [2009-03-27 02:56 UTC] 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.
 [2009-03-28 01:25 UTC] lbarnaud@php.net
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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 06 18:01:35 2024 UTC