Problems with writing a stream wrapper to regular fopen/close functions...

From: Date: Mon, 30 Aug 2004 02:17:38 +0000
Subject: Problems with writing a stream wrapper to regular fopen/close functions...
Groups: php.general 
Request: Send a blank email to php-general+get-195139@lists.php.net to get a copy of this message
Is there some fundamental reason that I'm missing that makes this impossible? Summary: I'm writing a custom stream to wrap around regular fopen/close functions (the actual stream will have more, but this is the part I'm having an issue with). The stream_open() method creates a class variable holding a file handle created with fopen(). $this->fp = fopen($path,$mode); A var_dump() of $this->fp shows a valid stream resource. However, if I try to use that resource during stream_read, stream_close, stream_eof, etc, it's no longer valid and has been converted to a bool(true) value. However, another class variable created in the stream_open method retains it's value throughout the other methods. Is this some kind of serialization/sleep/??? issue? That's the summary, here's the code: This is the file that defines the custom stream and some of the implementation. Not all methods are implemented, but I don't think that matters to this issue. -------------------- Custom Stream Class: <?php stream_wrapper_register('cache','CacheStream'); class CacheStream {
    public $fp = FALSE;
    public $foo = FALSE;
    function stream_open($path, $mode, $options='', &$opened_path='')
    {
        $path = substr($path,8);
        $this->fp = fopen($path,$mode);
        $this->foo = ' Test String ';
        var_dump($this->fp);
        var_dump($this->foo);
        return $this->fp;
    }
    function stream_close()
    {
        var_dump($this->fp);
        var_dump($this->foo);
        return fclose($this->fp);
    }
    function stream_read($bytes)
    {
        var_dump($this->fp);
        var_dump($this->foo);
        return fread($this->fp,$bytes);
    }
    function stream_eof()
    {
        var_dump($this->fp);
        var_dump($this->foo);
        if(feof($this->fp))
        { return TRUE; }
        else
        { return FALSE; }
    }
} ?> -------------------- This is the test file that implements the custom stream. It first starts with the regular fopen/close functions to show it works with the test.rss file, then moves on to the custom stream. -------------------- Test.php <?php error_reporting(E_ALL); include('streamtest.php'); if($fp1 = fopen('test.rss','r')) { echo 'File1 open<br />'; } if($c = fread($fp1,20)) { echo 'Content1: ' . htmlentities($c) . '<br />'; } if(fclose($fp1)) { echo 'File1 closed<br />'; } if($fp2 = fopen('cache://test.rss','r')) { echo 'File2 open<br />'; } if($c = fread($fp2,20)) { echo 'Content2: ' . htmlentities($c); } if(fclose($fp2)) { echo 'File2 closed<br />'; } ?> -------------------- This is the result: -------------------- File1 open Content1: <?xml version="1.0" File1 closed resource(6) of type (stream) string(13) " Test String " File2 open bool(true) string(13) " Test String " Warning: fread(): supplied argument is not a valid stream resource in c:\program files\apache group\Apache\htdocs\zorin\streamtest.php on line 31 bool(true) string(13) " Test String " Warning: feof(): supplied argument is not a valid stream resource in c:\program files\apache group\Apache\htdocs\zorin\streamtest.php on line 38 bool(true) string(13) " Test String " Warning: fclose(): supplied argument is not a valid stream resource in c:\program files\apache group\Apache\htdocs\zorin\streamtest.php on line 24 File2 closed -------------------- -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals – www.phparch.com

« previous php.general (#195139) next »