如何使用exec方法启动exe应用程序并在PHP中传递参数?

时间:2016-01-12 18:56:15

标签: php shell command-line

这听起来很简单,但我必须在这里遗漏一些东西。我有一个自定义exe程序,它位于C:\dummy\dummytest.exe内,我在 C:\text\test.txt 内有一个文本文件。我想要做的就是通过在PHP中传递test.txt作为参数来启动dummytest.exe。这是我尝试过的:      $arg = "C:\text\test.txt" exec("C:\dummy\dummytest.exe".$arg); 我也试过单一的'\'。我试过了     exec("C:\dummy\dummytest.exe $arg");但似乎没有任何效果。我得 C:\dummy is not recognized as an internal or external command, operable or batch file error. 当我手动转到命令行并执行操作时     C:\dummy\dummytest.exe test.txt 应用程序运行得很好。我和exec在这里错过了什么?

1 个答案:

答案 0 :(得分:2)

使用" shell_exec"而不是命令。

shell_exec("[BAT or EXE-File] [Params]");

希望这有帮助!

修改

使用带有路径的可执行文件和参数时,必须引用它们。 所以,一个例子看起来像这样:

echo nl2br(shell_exec("\"F:\\N3V Games\\Trainz Simulator 12\\compile_gs.bat\" \"F:\\xampp\\htdocs\\SHELL\\EBuLa.gs\""));

此示例将CMD-Window的输出直接打印到页面。

如果可执行文件放在与php文件相同的目录中,则可以运行:

echo nl2br(shell_exec("compile_gs.bat EBuLa.gs"));