ID: 47397 Updated by: j...@php.net Reported By: shaunspiller at gmail dot com -Status: Open +Status: Feedback -Bug Type: *General Issues +Bug Type: CGI related Operating System: Any? PHP Version: 5.2.9RC2 New Comment:
1. Exactly what PHP version are you using here? 2. What if you don't open another stdout stream but use the STDOUT constant, does it work better..? For more info: http://www.php.net/wrappers.php Previous Comments: ------------------------------------------------------------------------ [2009-02-15 19:16:22] shaunspiller at gmail dot com Description: ------------ Hi! I think this might be a bug. I was writing some code that used output buffering, but which also bypassed it by writing directly to stdout. I've done before it under CLI but the results I got under CGI and as an Apache module were a bit strange: Example 1: ---------- <?php echo "1. echo, before output buffering<br>\n"; ob_start(); echo "2. echo, during output buffering<br>\n"; flush(); /* in theory, this line will be output immediately while 2 & 4 will be held back until ob_end_flush() */ $stdout = fopen('php://stdout', 'w'); fwrite($stdout, "3. fwrite to stdout, during output buffering<br>\n"); echo "4. echo, during output buffering<br>\n"; ob_end_flush(); echo "5. echo, after output buffering<br>\n"; ?> Result: ------- I'm not expert on how PHP communicates with its various output mechanisms. These are just my observations from testing this code: * Under CLI, this example works, and the displayed order is 1, 3, 2, 4, 5. * As an Apache module, no. 3 is never output, no matter how much I try to flush it through. (Maybe that is the intended behavior, since the STDOUT constant is not defined.) * Under CGI, no. 3 is never output.... **unless** at least 1 previous byte has been flushed (provided by the echo()s and flush() call, above). In that case, the displayed order is 1, 3, 2, 4, 5 again. I'm not sure if it's supposed to work or not, but the inconsistency seems wrong. * (In all cases, the fopen call returns a valid stream.) Example 2: ---------- <?php header('Cache-Control: no-cache'); $stdout = fopen('php://stdout', 'w'); fwrite($stdout, "Location: http://www.php.net/\r\n"); ?> Result: ------- This is even stranger. Under CGI, if at least one call to header() has been made and no other data has yet been flushed, writing to stdout puts data directly into the HTTP response. In this case I've used a complete valid header so it can be tested from a browser. It's also possible to write complete garbage into the headers, bypassing the header() function's restrictions (this is best observed via telnet), and this is what was unintentionally happening when I first encountered this. Expected result: ---------------- It would be nice if stdout would always work, and always bypass output buffering. Otherwise, it should at least be consistent within each interface. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=47397&edit=1