Bug #77828 [Opn]: SplFileObject::seek problems on custom stream wrappers
| From: | requinix@php.net | Date: | Sun, 31 Mar 2019 23:19:56 +0000 |
| Subject: | Bug #77828 [Opn]: SplFileObject::seek problems on custom stream wrappers | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-220272@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77828&edit=1
ID: 77828
Updated by: requinix@php.net
Reported by: alexey dot khokhryakov at gmail dot com
Summary: SplFileObject::seek problems on custom stream
wrappers
Status: Open
Type: Bug
Package: SPL related
PHP Version: 7.2.16
Block user comment: N
Private report: N
New Comment:
It's not SplFileObject but your stream wrapper. PHP's file wrapper is reading lines, yours
is reading bytes. If you edit yours to read lines as well
public function stream_read($count) { return fgets($this->r, $count); }
then it works as expected.
Not sure the best way to fix this.
Previous Comments:
------------------------------------------------------------------------
[2019-03-31 22:32:33] alexey dot khokhryakov at gmail dot com
Description:
------------
SplFileObject::seek() works wrong for custom stream wrappers. It should work exactly the same as for
native files.
In my example I created simple stream wrapper which just proxies all calls to native
fopen/fread/ftell/feof/fseek/stat functions which demonstrates the problem.
Test script:
---------------
<?php
file_put_contents($tmpFilename = tempnam(sys_get_temp_dir(), 'test'), "one
line\r\n");
stream_wrapper_register('foo', FooStreamWrapper::class);
foreach ([$tmpFilename, "foo:/{$tmpFilename}"] as $filename) {
$file = new SplFileObject($filename, 'r');
$file->seek($file->getSize());
var_dump($file->key());
}
class FooStreamWrapper {
private $r;
public function stream_open($path, $mode) { $this->r = fopen(str_replace('foo:/',
'', $path), $mode); return true; }
public function stream_read($count) { return fread($this->r, $count); }
public function stream_tell() { return ftell($this->r); }
public function stream_eof() { return feof($this->r); }
public function stream_seek($offset, $whence) { return fseek($this->r, $offset, $whence) ===
0; }
public function url_stat($path) { return stat(str_replace('foo:/', '',
$path)); }
}
Expected result:
----------------
int(1)
int(1)
Actual result:
--------------
int(1)
int(0)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77828&edit=1