Weird error in PHP, spaces in paths and Windows

I need to fix this little mistake. First, let me talk about a small fact: In the Windows CLI, you cannot run a program with a space along its path unless you escape:

C:\>ab/c.bat 'a' is not recognized as an internal or external command, operable program or batch file. C:\>"ab/c.bat" C:\> 

I use proc_open ... proc_close in PHP to start a process (program), for example:

 function _pipeExec($cmd,$input=''){ $proc=proc_open($cmd,array(0=>array('pipe','r'), 1=>array('pipe','w'),2=>array('pipe','w')),$pipes); fwrite($pipes[0],$input); fclose($pipes[0]); $stdout=stream_get_contents($pipes[1]); // max execusion time exceeded ssue fclose($pipes[1]); $stderr=stream_get_contents($pipes[2]); fclose($pipes[2]); $rtn=proc_close($proc); return array( 'stdout'=>$stdout, 'stderr'=>$stderr, 'return'=>(int)$rtn ); } // example 1 _pipeExec('C:\\ab\\c.bat -switch'); // example 2 _pipeExec('"C:\\ab\\c.bat" -switch'); // example 3 (sounds stupid but I had to try) _pipeExec('""C:\\ab\\c.bat"" -switch'); 

Example 1

  • RESULT: 1
  • STDERR: 'C: \ a' is not recognized as an internal or external command, operating program, or batch file.
  • STDOUT:

Example 2

  • RESULT: 1
  • STDERR: 'C: \ a' is not recognized as an internal or external command, operating program, or batch file.
  • STDOUT:

Example 3

  • RESULT: 1
  • STDERR: Incorrect file name, directory or volume name.
  • STDOUT:

So, you see that in any case (double quotes or not) the code does not work. Is it me or am I missing something?

+6
windows command-line-interface php path space
source share
3 answers

Unfortunately, the fix does not work as expected, however the first Pekka suggestion gave me an idea:

 $file='C:\ab\c'; $cmdl='/d /b /g'; if(strtolower(substr(PHP_OS,0,3))=='win') // if windows... $file='cd '.escapeshellarg(dirname($file)).' && '.basename($file); _pipeExec($file.' '.$cmdl); 

It depends on the platform, and I hope that I don’t have to fix it compared to linux. So far, it works well!

+3
source share

Another way to resolve this issue is to add extra double quotes at the beginning and end of the command.

 $process = 'C:\\Program Files\\nodejs\\node.exe'; $arg1 = 'C:\\Path to File\\foo.js'; $cmd = sprintf('"%s" %s', $process, escapeshellarg($arg1)); if (strtolower(substr(PHP_OS, 0, 3)) === 'win') { $cmd = '"'.$cmd.'"'; } _pipeExec($cmd); 

I found this solution at https://bugs.php.net/bug.php?id=49139
It looks weird, but hey it's Windows ...: D

+1
source share

This is misery.

Pending workarounds:

  • Use a temporary environment variable:

     exec('SET ENVPATH="C:\ab"'); proc_open('%ENVPATH%\c.bat' .... 

    (don't know if this will work for proc_open)

  • Use the file name 8.3, if it can be obtained in some way in PHP - it can be done with another exec()

  • proc_open() has the ability to bypass cmd.exe - it might be worth a try if the file system somehow handles quotes differently

  • Try quoting \"

0
source share

All Articles