为什么 exec() 会因空消息字符串而失败?

why does exec() fail with empty message-strings?

我在 Windows 服务器上使用 XAMPP(不在办公室了,明天会添加准确的版本)。

今天我偶然发现了这个非常奇怪的行为: 当使用 exec() 执行程序时,一些命令可以运行,而另一些则没有给出任何原因就失败了。

//working
exec("dir", $output, $retval);

//$retval = 0;
//$output = array with response-lines


它似乎也适用于我的 wkhtmltopdf.exe:

//working as well
exec("C:\some_path\wkhtmltopdf.exe --help", $output, $retval);

//$retval = 0;
//$output = array with lines from the help-file


但是一旦变得有点复杂,它就会失败:

//not working
exec("C:\some_path\wkhtmltopdf.exe C:\other_path\test.html C:\target_path\test.pdf", $output, $retval);

//$retval = 1;
//$output = array with 11 empty strings ?!?!


当我使用 rdp 将完全相同的字符串复制到服务器计算机,并在 windows-shell (cmd) 中使用它时,它起作用了。

我不知道发生了什么 - 恕我直言,很奇怪我得到了一个包含 11 个空字符串的数组。

感谢您的帮助或提示!

我认为问题在于参数中的斜杠:

$input  = 'C:\other_path\test.html';
$target = 'C:\target_path\test.pdf';

exec("C:\some_path\wkhtmltopdf.exe '$input' '$target'", $output, $retval);

您不必像这样编写代码,但请尝试使用 '.

封装路径

以上代码结果为:

exec("C:\some_path\wkhtmltopdf.exe 'C:\other_path\test.html' 'C:\target_path\test.pdf'", $output, $retval);

您也可以尝试转义斜杠:

exec("C:\some_path\wkhtmltopdf.exe C:\other_path\test.html C:\target_path\test.pdf", $output, $retval);

但那是一个混乱的代码。