Edit report at https://bugs.php.net/bug.php?id=60671&edit=1
ID: 60671 Updated by: cataphr...@php.net 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 +Status: Bogus Type: Bug Package: Streams related Operating System: Ubuntu 11.04 PHP Version: 5.3.8 Block user comment: N Private report: N New Comment: This is not a bug. fread only returns false if given invalid arguments. The bug is that you try to read from a stream that's write-only. This C program has analogous behavior: #include<stdio.h> int main(void) { FILE *f = fopen("/tmp/foobaz", "w"); printf("feof: %d\n", feof(f)); printf("read: %zd\n", fread((char[100]) {}, 1, 100, f)); printf("feof: %d\n", feof(f)); return 0; } gcc --std=c99 h.c && ./a.out feof: 0 read: 0 feof: 0 Previous Comments: ------------------------------------------------------------------------ [2012-01-06 16:01:07] phpmpan at mpan dot pl 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. ------------------------------------------------------------------------ [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