|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60131 fopen('php://stdout'...) redirect all posterious echo to /dev/null
Submitted: 2011-10-25 14:24 UTC Modified: 2011-10-30 03:14 UTC
Avg. Score:5.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: frederic dot hardy at mageekbox dot net Assigned:
Status: Wont fix Package: Streams related
PHP Version: 5.3.8 OS: Mac OS Snow Leopard
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
18 - 4 = ?
Subscribe to this entry?

 [2011-10-25 14:24 UTC] frederic dot hardy at mageekbox dot net
If fopen('php://stdout', ...) is used in a php processus open with proc_open(), 
all posterious call to echo or print seems to be redirected to /dev/null.

Test script:

$descriptors = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w")

$php = proc_open('php', $descriptors, $pipes);

stream_set_blocking($pipes[1], 0);

fwrite($pipes[0], '<?php echo \'foo\'; fopen(\'php://stdout\', \'w\'); echo \'bar\'; ?>');

$null = null;

$select = array($pipes[1]);

while (feof($pipes[1]) === false)
	if (stream_select($select, $null, $null, null))

Expected result:

Actual result:


bug60131.diff (last revision 2011-10-30 03:14 UTC by

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-30 03:09 UTC]
Won't fix since some people rely on this behavior. What happens is the first time you fopen stdout, the file descriptor 1 is not duped and exposed with a stream, instead the stdlib FILE* is exposed through a stream. When the stream is closed, the FILE* is closed too, potentially also closing stdout, i.e., a close(1) syscall (which is what happens in your case – since you don't assign the return of fopen, the stream is immediately destroyed). Turns out some people use this technique to close the standard file descriptors and open others (and AFAIK there's no other way to do it in PHP).
 [2011-10-30 03:09 UTC]
-Status: Open +Status: Wont fix
 [2011-10-30 03:14 UTC]
The following patch has been added/updated:

Patch Name: bug60131.diff
Revision:   1319944441
 [2011-10-30 03:14 UTC]
For the record, I added a patch that would fix this problem.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 17 05:01:34 2024 UTC