Edit report at https://bugs.php.net/bug.php?id=60671&edit=1
ID: 60671
Comment by: phpmpan at mpan dot pl
Reported by: james dot turner dot phpninja at gmail dot com
Summary: fread does not fail when operating on a write only
stream
Status: Open
Type: Bug
Package: Streams related
Operating System: Ubuntu 11.04
PHP Version: 5.3.8
Block user comment: N
Private report: N
New Comment:
CONFIRMED for both 5.3.8 and 5.3.7 on Arch64, and for 5.3.4 on an unknown Linux.
Note however, that the test script provided by OP is wrong. It should be:
------------ BEGIN CODE ------------
$tmp = tempnam(sys_get_temp_dir(), 'test_');
$stream = fopen($tmp, 'w');
$data = "";
while(!feof($stream)){
if(false === ($data = fread($stream, 8192))){
break; // ^---- no dot here
};
}
------------- END CODE -------------
OP's code will fail regardless of the bug, because .= always produces a string.
Previous Comments:
------------------------------------------------------------------------
[2012-01-06 14:29:43] james dot turner dot phpninja at gmail dot com
Description:
------------
fread does not throw or generate any error when attempting to read from a write
only file stream.
Test script:
---------------
<?php
$tmp = tempnam(sys_get_temp_dir(), 'test_');
$stream = fopen($tmp, 'w');
$data = "";
while(!feof($stream)){
if(false === ($data .= fread($stream, 8192))){
break;
};
}
?>
Expected result:
----------------
Either feof to return false indicating end of file
Or fread erroring or returning false as a result of attempting to read a
write-only stream.
Actual result:
--------------
An infinite loop will occur.
feof will never end (doesn't reach the end of the file because it's in write
mode)
fread will never error despite attempting to read from a write only stream.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60671&edit=1