php.net |  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
Votes:1
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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: frederic dot hardy at mageekbox dot net
New email:
PHP Version: OS:

 

 [2011-10-25 14:24 UTC] frederic dot hardy at mageekbox dot net
Description:
------------
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:
---------------
<?php

$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\'; ?>');
fclose($pipes[0]);

$null = null;

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

while (feof($pipes[1]) === false)
{
	if (stream_select($select, $null, $null, null))
	{
		var_dump(stream_get_contents($pipes[1]));
	}
}

Expected result:
----------------
foobar

Actual result:
--------------
foo

Patches

bug60131.diff (last revision 2011-10-30 03:14 UTC by cataphract@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-30 03:09 UTC] cataphract@php.net
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] cataphract@php.net
-Status: Open +Status: Wont fix
 [2011-10-30 03:14 UTC] cataphract@php.net
The following patch has been added/updated:

Patch Name: bug60131.diff
Revision:   1319944441
URL:        https://bugs.php.net/patch-display.php?bug=60131&patch=bug60131.diff&revision=1319944441
 [2011-10-30 03:14 UTC] cataphract@php.net
For the record, I added a patch that would fix this problem.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 16:01:31 2024 UTC