|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-11-22 23:14 UTC] tim_siebels_aurich at yahoo dot de
Description:
------------
To get `feof` return true you need to read `length+1` bytes you wrote. Or in general it returns true after you tried to read beyond the last byte.
the `php://temp` (and php://memory too) stream however returns true after `length` bytes read.
Test script:
---------------
<?php
// Test File Stream
$file = tempnam('/tmp', 'feoftest');
$h = fopen("php://temp", 'r+');
fwrite($h, "bug");
fseek($h, 0);
fread($h, 3);
var_dump(feof($h));
// Test Temp Stream
$h = fopen("php://temp", 'r+');
fwrite($h, "bug");
fseek($h, 0);
fread($h, 3);
var_dump(feof($h));
Expected result:
----------------
bool(false)
bool(false)
Actual result:
--------------
bool(false)
bool(true)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 09:00:01 2025 UTC |
Maybe relevant, PHP & HHVM behave differently on `feof()`. When `length` bytes are read, `feof()` returns `true` on PHP while HHVM still returns `false`. See code below: ```php $stream = fopen('php://temp', 'r+'); fwrite($stream, "HelloWorld"); rewind($stream); $result = fread($stream, 10); echo $result . "\n"; // HelloWorld var_dump(feof($stream)); // with PHP: `true`, with HHVM: `false` ``` They both returns false after reading `length + 1` bytes. Would be great to have something consistent.<?php // yep, it's a bug!!! $f = tmpfile(); fwrite($f, 'crap' ); // 4 bytes rewind($f); $i=1; while( !feof($f) ) { fread($f,1); echo "$i\n"; $i++; } /* outputs: 1 2 3 4 5 why the hell 5 bytes? should normally output 4 bytes: 1 2 3 4 */ ?>