我必须修复这个小错误。首先,我们先来说一个小事实:在 Windows 上的 CLI 中,您无法运行路径中带有空格的程序,除非进行转义:

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

C:\>"a b/c.bat"

C:\>

我在 PHP 中使用 proc_open...proc_close 来运行进程(程序),例如:

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:\\a b\\c.bat -switch');
// example 2
_pipeExec('"C:\\a b\\c.bat" -switch');
// example 3 (sounds stupid but I had to try)
_pipeExec('""C:\\a b\\c.bat"" -switch');

实施例1

  • 结果:1
  • 标准错误:“C:\a”未被识别为内部或外部命令, 可操作的程序或批处理文件。
  • 标准输出:

实施例2

  • 结果:1
  • 标准错误:“C:\a”未被识别为内部或外部命令, 可操作的程序或批处理文件。
  • 标准输出:

实施例3

  • 结果:1
  • 标准错误:文件名、目录名或卷标语法不正确。
  • 标准输出:

所以你看,无论哪种情况(双引号与否)代码都会失败。是我还是我错过了什么?

有帮助吗?

解决方案

最不幸的是,修复并没有按预期工作,但是 Pekka 的第一个建议给了我一个想法:

$file='C:\a b\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);

这是特定于平台的,我希望我也不必在 Linux 上修复这个问题。到目前为止效果很好!

其他提示

解决此问题的另一种方法是在命令的开头和结尾添加额外的双引号。

$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);

我在上找到了这个解决方案 https://bugs.php.net/bug.php?id=49139
看起来很奇怪,但是嘿 - 这是 Windows...:D

这是斯特雷特。

未经测试的解决方法想法:

  • 使用临时环境变量:

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

    (不知道这是否适用于 proc_open)

  • 如果可以以某种方式在 PHP 中获取该文件名,请使用 8.3 文件名 - 使用另一个文件名肯定是可行的 exec()

  • proc_open() 有一个选项可以绕过 cmd.exe - 如果文件系统以某种方式以不同方式处理引号,可能值得一试

  • 尝试转义引号 \"

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top