|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41874 Separate STDOUT and STDERR in exec functions
Submitted: 2007-07-02 18:39 UTC Modified: 2009-06-09 14:07 UTC
Avg. Score:4.5 ± 1.1
Reproduced:10 of 11 (90.9%)
Same Version:3 (30.0%)
Same OS:10 (100.0%)
From: ckeefer at us dot nomura dot com Assigned: pajoye (profile)
Status: Closed Package: Program Execution
PHP Version: 5CVS, 6CVS (2008-11-01) OS: win32 only
Private report: No CVE-ID: None
 [2007-07-02 18:39 UTC] ckeefer at us dot nomura dot com
PHP CLI version 5.1.8 thru 5.2.1 Windows Binary - exec() function, when error is generated passes STDERR no problem to CLI window.  5.2.2+ STDERR output has vanished.  Nothing in documentation or search suggests what happened.  No other changes 5.2.1 just downloaded and tested with generic setup (no php.ini or other modifications) stderr from exec() functions perfectly.  5.2.2+ system() apparently passes stderr properly, but is not useful for most of what we do. STDOUT appears to be fine all versions.

Oddly, 5.2.2+ versions still work correctly with cygwin/bash 3.1

Note in 5.2.2 changelog contains this bit:
- Fixed commandline handling for CLI and CGI. (Marcus, Johannes)


Chip Keefer

Reproduce code:
$result = exec('cd c:\non_existant; dir nonexistant');
echo "$result\n";

Expected result:
NJPC3916:php5.22$ php test_exec.php 
php test_exec.php 
The system cannot find the path specified.

- Works properly with cygwin

Actual result:
C:\php5.22>php test_exec.php




Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-03 12:56 UTC] ckeefer at us dot nomura dot com
Thank you for the quick response.  The new version still shows no STDERR output from a cmd.exe window.


C:\php5.2fix>php test_exec.php
Two exec statements:
This exec should result in error for non-existant dir:
This exec works:
    [0] =>  Volume in drive C is LOCAL DISK
    [1] =>  Volume Serial Number is 4054-8345
    [2] =>
    [3] =>  Directory of C:\php5.2fix
    [4] =>
    [5] => 07/03/2007  08:08 AM            32,821 php.exe
    [6] =>                1 File(s)         32,821 bytes
    [7] =>                0 Dir(s)  58,549,849,088 bytes free

C:\php5.2fix>type test_exec.php
echo "Two exec statements:\n-------------------\n";
echo "This exec should result in error for non-existant dir:\n";
exec("cd c:\\non_existant", $err);
echo "-----------------------\n";
echo "This exec works:\n--------------------\n";
exec("dir php.exe", $ary);
 [2007-07-18 08:45 UTC]
This works fine with *nix (found from the user comments at :


$com= "ls foo";

exec("$com 2>&1", $out, $err);



I don't know if Windows supports something like this, but iirc, it should.

 [2007-07-18 13:01 UTC] ckeefer at us dot nomura dot com
>Oddly, 5.2.2+ versions still work correctly with cygwin/bash 3.1
Using the same Win32 binary, not a Cygwin binary.

To  That is a good suggestion for just getting STDERR output and I use it often on Unix and Linux systems when I need, and don't care if outputs are mixed.  I haven't tested if this construct works with W32.  Unfortunately, I need STDIN and STDERR separate.

I also have a question:  Did the version of compiler or libraries used to compile the binary change? MS is notorious for making undocumented changes to their development tools.

Chip Keefer
 [2007-07-18 14:55 UTC]
Since this is actually improvement from previous behaviour where the output was mixed, I'm changing this to a FR. I'll look into adding an extra parameter to store STDERR in. In the meantime I suggest you use proc_open() or popen() which give you more control over these..
 [2007-07-18 15:47 UTC] ckeefer at us dot nomura dot com
I don't actually see how this is an improvement as output was only mixed if you don't redirect, which is expected.  If I do something like php progname.php > logfile.txt, I would previously redirect STDIN to the logfile as expected and stderr would still come to the console so I could monitor the program's progress.  This is the usual behavior in shell programs, etc.  This is how cmd line php was working through version 5.21.  5.22 on changed behavior in that from Win32, the STDERR disappears completely.  If you can tell me where STDERR went and how to get to it from exec, I will happily do that.  I have hundreds (if not thousands) of php scripts that do this.  This new behavior breaks all of them.  If I do php progname.php 2>&1 > logfile.txt, even if this works, I get mixed STDIN and STDERR output in the logfile.txt.  This will force me to either stay at version 5.21 or earlier, or revise every script we have that currently uses the exec function.

Again I ask: Did anything change with the compiler or other development tools used to create the Window Binary?  Microsoft has been known to modify libraries in undocumented ways.

Chip Keefer
 [2007-07-19 20:00 UTC]
As I'm not any win32 expert (nor really care about it) I'll deassign this and forget that change, perhaps someone else who know the win32 stuff picks this up.
 [2008-10-24 16:13 UTC]
Assigned to the Windows port maintainer.
 [2009-06-09 14:07 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

Fixed in 5.3+
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Mon Jul 16 10:01:53 2018 UTC