From:             ivan dot enderlin at hoa-project dot net
Operating system: 
PHP version:      master-Git-2013-08-29 (Git)
Package:          *General Issues
Bug Type:         Bug
Bug description:stream_get_contents does not seek with a stream wrapper

Description:
------------
When calling stream_get_contents() with $offset >= ftell() *through a
stream wrapper*, the internal pointer of the stream is moved but
stream_get_contents() throws an error: "Failed to seek to position $offset
in the stream".

While monitoring the source code (ext/standard/streamsfuncs.c, at line 404,
no kidding ;-)), it appears that it is a normal behaviour since seek_res is
set to -1 at line 426 (with the code bellow). I did not understand why (I
looked at php_stream_seek implementation but I was not able to
understand).

Change fopen('foo://bar', 'rb') for fopen(__FILE__, 'rb') and the issue
disappears. That's why I think it is related to stream wrapper, but I
cannot ensure that.

Test script:
---------------
<?php

class StreamWrapper {

    protected $_stream = null;

    public function stream_open ( $path, $mode, $options, &$openedPath ) {

        $this->_stream = fopen(__FILE__, $mode);

        return true;
    }

    public function stream_seek ( $offset, $whence = SEEK_SET ) {

        var_dump('seek to ' . $offset);

        return fseek($this->_stream, $offset, $whence);
    }

    public function stream_read ( $count ) {

        return fread($this->_stream, $count);
    }

    public function stream_stat ( ) {

        return fstat($this->_stream);
    }

    public function stream_eof ( ) {

        return feof($this->_stream);
    }
}

stream_wrapper_register('foo', 'StreamWrapper');

$a = fopen('foo://bar', 'rb');
var_dump(stream_get_contents($a, 30, 4));


Expected result:
----------------
no error

Actual result:
--------------
error

-- 
Edit bug report at https://bugs.php.net/bug.php?id=65581&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=65581&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=65581&r=trysnapshot53
Try a snapshot (trunk):     
https://bugs.php.net/fix.php?id=65581&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=65581&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=65581&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=65581&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=65581&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=65581&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=65581&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=65581&r=notwrong
Not enough info:            
https://bugs.php.net/fix.php?id=65581&r=notenoughinfo
Submitted twice:            
https://bugs.php.net/fix.php?id=65581&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=65581&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65581&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=65581&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=65581&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=65581&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=65581&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=65581&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=65581&r=mysqlcfg

Reply via email to