php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #78699 shell_exec on windows runs in text mode, this should be noted in the docs.
Submitted: 2019-10-20 05:35 UTC Modified: 2019-10-20 10:37 UTC
From: phpstuff at cresstone dot com Assigned: cmb (profile)
Status: Closed Package: Program Execution
PHP Version: 7.3.10 OS: Windows
Private report: No CVE-ID: None
 [2019-10-20 05:35 UTC] phpstuff at cresstone dot com
Description:
------------
---
From manual page: https://php.net/function.shell-exec
---

On windows, shell_exe() & the backtick operator operate in text mode. When I was trying to get binary stdout from a command I encountered incorrect output, and the command I was running gave ‘broken pipe’ messages on its stderr. Any binary output that includes newlines seem to cause errors.

The documentation doesn’t mention this, and took me a while to puzzle out what was happening.


The work-around is to use popen() with the binary flag:

$handle = popen('someCommand.exe', 'rb');
$binarySTDOUT = stream_get_contents($handle); 
pclose($handle);

I wasn’t sure whether to file this as a language bug or just add a comment on the documentation, this report seemed like a good compromise...



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-10-20 07:30 UTC] phpstuff at cresstone dot com
Edit:
The correct workaround code should be:
$handle = popen('someCommand.exe', 'rb');
$binarySTDOUT="";
while (!feof($handle)) 
    $binarySTDOUT.= fread($handle, 65536);
pclose($handle);

stream_get_contents() does not always produce the correct data for me. I'll investigate if that's a separate issue...
 [2019-10-20 07:39 UTC] phpstuff at cresstone dot com
Disregard that last edit, stream_get_contents works fine and I've been at the keyboard too long.
 [2019-10-20 08:05 UTC] cmb@php.net
-Status: Open +Status: Verified -Package: Documentation problem +Package: Program Execution
 [2019-10-20 08:05 UTC] cmb@php.net
Indeed, you're right.  I think treating this as documentation
issue is the right way.  Thanks for reporting!
 [2019-10-20 10:37 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&revision=348185
Log: Fix #78699: shell_exec on windows runs in text mode, this should be noted in the docs
 [2019-10-20 10:37 UTC] cmb@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: cmb
 [2019-10-20 10:37 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2019-10-20 10:40 UTC] salathe@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=23511dbcee471b8d3db718857bec82e1be0d5222
Log: Fix #78699: shell_exec on windows runs in text mode, this should be noted in the docs
 [2020-02-07 06:04 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=6a9de0cb6ac1928c8347bb2af2320eef7370c8ae
Log: Fix #78699: shell_exec on windows runs in text mode, this should be noted in the docs
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Apr 21 21:01:24 2021 UTC