Redirect PHP STDERR when used on the command line in Windows

I'm running a PHP script from the command line, and I need to capture any error output. The command is something like this:

|php -f <phpscriptfile> 2>&1

Under Linux, this works as expected. Under Windows, it doesn't. I don't know where the error output goes, but it's not coming back to me on STDOUT. If I run this from a DOS box and specifically redirect 2> to a file, it does not go to the file, it displays on the screen.

Interestingly enough, if I misspell 'php' on the command line, the DOS error message is redirected.

Also, if I misspell the script name, that php error is redirected. It's only errors that occur during execution that don't get redirected.


php config info:

phpinfo()
PHP Version => 5.3.10

System => Windows NT ROCKET 6.0 build 6002 (Windows Server 2008 Standard Edition Service Pack 2) i586
Build Date => Feb  2 2012 20:10:58
Compiler => MSVC9 (Visual C++ 2008)
Architecture => x86
Configure Command => cscript /nologo configure.js  "--enable-snapshot-build" "--enable-debug-pack" "--disable-zts" "--disable-isapi" "--disable-nsapi" "--without-mssql" "--without-pdo-mssql" "--without-pi3web" "--with-pdo-oci=C:\php-sdk\oracle\instantclient10\sdk,shared" "--with-oci8=C:\php-sdk\oracle\instantclient10\sdk,shared" "--with-oci8-11g=C:\php-sdk\oracle\instantclient11\sdk,shared" "--with-enchant=shared" "--enable-object-out-dir=../obj/" "--enable-com-dotnet" "--with-mcrypt=static" "--disable-static-analyze"
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => C:\Windows
Loaded Configuration File => (none)
Scan this dir for additional .ini files => (none)
Additional .ini files parsed => (none)
PHP API => 20090626
PHP Extension => 20090626
Zend Extension => 220090626
Zend Extension Build => API220090626,NTS,VC9
PHP Extension Build => API20090626,NTS,VC9
Debug Build => no
Thread Safety => disabled
Zend Memory Manager => enabled
Zend Multibyte Support => disabled
IPv6 Support => enabled
Registered PHP Streams => php, file, glob, data, http, ftp, zip, compress.zlib, phar  
Registered Stream Socket Transports => tcp, udp
Registered Stream Filters => convert.iconv.*, mcrypt.*, mdecrypt.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk, zlib.*

Wasn't sure if you need anything else, so I just put the first bit of phpinfo here.

Any suggestions, anyone?

Thanks.
JPNeronAsked:
Who is Participating?
 
ahoffmannConnect With a Mentor Commented:
does it work if you use a foo.bat with

  php -f <phpscriptfile>

and then call

  foo.bat 2>&1
0
 
JPNeronAuthor Commented:
if I do:

foo.bat 2>null

the error is still displayed in the window.

if I do:

foot.bat 1>null 2>&1

then the error goes to a file called null

If I change foo.bat to:

 php -f <phpscriptfile> 1>null2

and run it via:

foo.bat 2>&1

then output goes to null2.

I really don't want to have to create a batch file to do this, if that's at all possible.

Thanks.
0
 
ahoffmannCommented:
I'm no M$ expert, but it sounds like you try to solve a problem where 2 highly proprietary (cmd.exe, php.exe) systems won't agree on common standards
however, php is open source, may be it can be solved there ...
sorry for no better answer
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
JPNeronAuthor Commented:
I was hoping there was a php.ini setting or something that addresses this. I can't be the first person to encounter this, can I?

Any other suggestions, anyone?

Thanks.
0
 
ahoffmannCommented:
can you change your script? then this may be a workaround:

fclose(STDERR);
define(STDERR,fopen('php://stdout','w'));
0
 
JPNeronAuthor Commented:
It might be technically possible to change it, but it comes from a 3rd party, so when we get changes, my change will be overwritten.
0
 
JPNeronAuthor Commented:
FYI: As an experiment, I tried that change, no difference.

It doesn't seem fixable, so I've rewritten my program to call the php script in a batch file, and applied redirection on the batch file. Then I have to read the redirected STDERR file to see if anything went wrong.

Thanks anyway.
0
 
JPNeronAuthor Commented:
The solution is not ideal, but seems to be the only way to do it.
0
 
ahoffmannCommented:
good luck with cumbersome workaround for M$ which *ix fixed in early seventies last millenium ;-)
Thanks for grading.
0
All Courses

From novice to tech pro — start learning today.