xref: /PHP-7.3/ext/standard/tests/file/bug27508.phpt (revision 782352c5)
1--TEST--
2Bug #27508 (userspace wrappers have bogus eof indicator)
3--FILE--
4<?php # vim:ft=php
5class FileStream {
6    public $fp;
7
8    function stream_open($path, $mode, $options, &$opened_path)
9    {
10        $url = urldecode(substr($path, 9));
11        $this->fp = fopen($url, $mode);
12
13        return true;
14    }
15
16    function stream_read($count)
17    {
18        return fread($this->fp, $count);
19    }
20
21    function stream_write($data)
22    {
23        return fwrite($this->fp, $data);
24    }
25
26    function stream_tell()
27    {
28        return ftell($this->fp);
29    }
30
31    function stream_eof()
32    {
33        if (!$this->fp) {
34            return true;
35        }
36        return feof($this->fp);
37    }
38
39    function stream_seek($offset, $whence)
40    {
41        return fseek($this->fp, $offset, $whence) == 0 ? true : false;
42    }
43}
44
45stream_wrapper_register("myFile", "FileStream")
46    or die("Failed to register protocol");
47
48$tmp_dir = __DIR__;
49$tn = tempnam($tmp_dir, 'foo');
50if (!$tn) {
51  die("tempnam failed");
52}
53
54$fp = fopen("myFile://" . urlencode($tn), "w+");
55if (!$fp) {
56  die("fopen failed");
57}
58
59fwrite($fp, "line1\n");
60fwrite($fp, "line2\n");
61fwrite($fp, "line3\n");
62
63debug_zval_dump(feof($fp));
64rewind($fp);
65echo ftell($fp) . "\n";
66debug_zval_dump(feof($fp));
67while ($fp && !feof($fp)) {
68    echo fgets($fp);
69}
70fclose($fp);
71
72unlink($tn);
73?>
74--EXPECT--
75bool(false)
760
77bool(false)
78line1
79line2
80line3
81