我正在尝试在 Windows 上的 XAMPP 环境中使用 PHP 的 exec 命令。
当我使用简单的命令行并键入
“c:\Program Files (x86)\任意目录\sometool.exe”“c:\temp\test.dat”
一切都很完美。
然后我像这样使用 PHP 的 exec 命令:
<?php
exec('"c:\Program Files (x86)\whatever directory\sometool.exe" c:\temp\test.dat');
?>
它也有效。 但是当我包含引号时,该工具似乎无法获取参数。这意味着
<?php
exec('"c:\Program Files (x86)\whatever directory\sometool.exe" "c:\temp\test.dat"');
?>
不起作用。我尝试过逃避以及一切。我找不到解决方案。但我在这里需要引号才能启用名称中带有空格的路径。你能指出我正确的方向吗?
最佳答案
将您的路径保存为变量..然后执行它。将路径用单引号引起来,如下所示。
$cmd = '"C:\my path with spaces\targetapp.exe" C:\mypathnospaces\targetfile.xxx';
或者 - 尝试对所有内容中的空格进行此修复,尝试在反斜杠上使用转义字符,这是未经测试的,请告诉我它是否有效。
$cmd = '"C:\my path with spaces\targetapp.exe" C:\\my other path with spaces\\targetfile.xxx';
exec($cmd)
另一个解决方案是将实际的 PATH 添加到您的 Windows 环境中,这样您就根本不需要调用该路径。
关于带引号的 PHP 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20617458/