Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ PHP 8.7 UPGRADE NOTES
to 16 by default. Set the stream context option max_filter_count to change
this.

- Streams:
. php://temp streams (and therefore SplTempFileObject) no longer report EOF
as soon as the last byte has been buffered. EOF is now only reported once a
read hits the end of the data, consistent with php://memory and plain
files. As a result, loops such as `while (!feof($fp)) { fgets($fp); }` or
iterating an SplTempFileObject perform one additional iteration at the
end, exactly as they already did for php://memory and plain files.

========================================
2. New Features
========================================
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
SplTempFileObject iterates like SplFileObject at end of file
--FILE--
<?php
$path = __DIR__ . '/SplTempFileObject_iteration_eof.txt';
$files = [
'SplFileObject' => new SplFileObject($path, 'w+'),
'php://memory' => new SplFileObject('php://memory', 'w+'),
'SplTempFileObject' => new SplTempFileObject(),
'SplTempFileObject(0)' => new SplTempFileObject(0),
];

foreach ($files as $name => $file) {
$file->fwrite("a\nb\nc\n");
$file->rewind();

$lines = [];
foreach ($file as $key => $line) {
$lines[] = $key . '=' . json_encode($line);
}
echo $name, ' foreach: ', implode(' ', $lines), "\n";

$file->rewind();
$lines = [];
while (!$file->eof()) {
$lines[] = json_encode($file->fgets());
}
echo $name, ' fgets: ', implode(' ', $lines), "\n";
}
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/SplTempFileObject_iteration_eof.txt');
?>
--EXPECT--
SplFileObject foreach: 0="a\n" 1="b\n" 2="c\n" 3=false
SplFileObject fgets: "a\n" "b\n" "c\n" ""
php://memory foreach: 0="a\n" 1="b\n" 2="c\n" 3=false
php://memory fgets: "a\n" "b\n" "c\n" ""
SplTempFileObject foreach: 0="a\n" 1="b\n" 2="c\n" 3=false
SplTempFileObject fgets: "a\n" "b\n" "c\n" ""
SplTempFileObject(0) foreach: 0="a\n" 1="b\n" 2="c\n" 3=false
SplTempFileObject(0) fgets: "a\n" "b\n" "c\n" ""
1 change: 1 addition & 0 deletions ext/spl/tests/SplFileObject/gh8273.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ while ($file->valid()) {
2: line 2
3: line 3
4: line 4
5:
3 changes: 2 additions & 1 deletion ext/standard/tests/filters/bug79984.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fclose($f2);
--EXPECT--
filter onCreate
filtered 8192 bytes.
filtered 128 bytes and closing. Stream has reached end-of-file.
filtered 128 bytes.
filtered 0 bytes and closing.
int(8320)
filter onClose
21 changes: 21 additions & 0 deletions ext/standard/tests/streams/temp_stream_eof.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
php://temp must not report EOF before a read hits the end of the data
--FILE--
<?php
foreach (['php://memory', 'php://temp', 'php://temp/maxmemory:2'] as $url) {
$h = fopen($url, 'w+');
fwrite($h, "a\nb\nc\n");
rewind($h);
echo $url, ":";
while (!feof($h)) {
$line = fgets($h);
echo " ", json_encode($line), feof($h) ? "(EOF)" : "";
}
echo "\n";
fclose($h);
}
?>
--EXPECT--
php://memory: "a\n" "b\n" "c\n" false(EOF)
php://temp: "a\n" "b\n" "c\n" false(EOF)
php://temp/maxmemory:2: "a\n" "b\n" "c\n" false(EOF)
7 changes: 5 additions & 2 deletions main/streams/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ static ssize_t php_stream_temp_write(php_stream *stream, const char *buf, size_t
static ssize_t php_stream_temp_read(php_stream *stream, char *buf, size_t count)
{
php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
size_t got;
ssize_t got;

assert(ts != NULL);

Expand All @@ -405,7 +405,10 @@ static ssize_t php_stream_temp_read(php_stream *stream, char *buf, size_t count)

got = php_stream_read(ts->innerstream, buf, count);

stream->eof = ts->innerstream->eof;
/* The inner stream is read greedily and may already hit EOF while still
* returning data. Only propagate EOF once no more data is available, so that
* php://temp reports EOF at the same point as php://memory and plain files. */
stream->eof = got <= 0 && ts->innerstream->eof;

return got;
}
Expand Down
Loading