从 PHP 文件调用 Windows 程序(通过命令提示符)
我尝试过多种方式调用 Windows 程序,但每次都得到相同的结果。
该程序在我的机器上打开(没有 GUI),但从未关闭,这意味着浏览器将永远加载。
但是,当通过命令行提示符手动执行查询字符串时,程序会关闭。不仅如此,程序实际上并没有执行
(它只是启动,即没有任何结果)。
我只是想知道通过 PHP 启动带有开关的程序的正确方法。
这是有效的查询字符串(执行后关闭程序):
"C:\Program Files (x86)\Softinterface, Inc\Convert PowerPoint\ConvertPPT.exe" /S
"C:\Users\Farzad\Desktop\upload\test.ppt" /T "C:\Users\Farzad\Desktop\upload\test.png" /C 18
I have tried calling a windows program several ways and I have gotten the same result each time.
The program opens up on my machine (without a GUI) but never closes each means that the browser is forever loading.
Though when executing the query string manually through the command line prompt the program closes. Not only that, but the program doesn't actually execute
(it is just launched i.e. there aren't any results).
I just want to know the proper way of starting a program with switches through PHP.
Here is the query string that works (closes the program after executing):
"C:\Program Files (x86)\Softinterface, Inc\Convert PowerPoint\ConvertPPT.exe" /S
"C:\Users\Farzad\Desktop\upload\test.ppt" /T "C:\Users\Farzad\Desktop\upload\test.png" /C 18
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果程序永远不会关闭,则 PHP 无法从
exec()
返回值。该程序必须关闭。以这种方式访问桌面上的文件可能会出现问题。它将以网络服务器定义的任何权限执行。http://php.net/manual/en/function.exec.php
您可能会考虑
proc_open()
的高级功能。它将使您能够访问所有必要的管道,但我认为这在这种情况下不会对您有帮助。If the program never closes, then PHP can't return a value from
exec()
. The program must close. Chances are there is a problem accessing your files on your desktop in this manner. It will be executed with whatever permissions the webserver has defined.http://php.net/manual/en/function.exec.php
You might consider the advanced functionality of
proc_open()
. It will give you access to all the necessary pipes, but I don't think that will help you in this situation.如果 Windows 计算机上的目标目录是 C:\Program Files (x86)\Softinterface, Inc\Convert PowerPoint\ConvertPPT.exe,则需要双引号其中包含空格字符的目录。
如果把它翻译成php的话,应该是这样的:
If the target directory on your Windows machine is C:\Program Files (x86)\Softinterface, Inc\Convert PowerPoint\ConvertPPT.exe, you need to double-quote the directories that have space character within them.
To translate it into php terms, it should be like this:
存储 Windows 桌面的物理目录属于您的用户配置文件文件夹。这意味着其他用户(包括运行 Apache 的用户,这是典型的“本地系统”)将没有适当的权限来读取和写入文件。虽然您可以调整 Apache 设置以使其与您自己的用户 Farzad 一起运行,但更常见的做法是将 Web 应用程序放在完全不同的目录树中。 ConvertPPT.exe 可能会因为尝试在不允许的位置写入文件而停止。我建议您创建一个顶级文件夹目录并确保它是全局可写的(完成后,如果您愿意,可以收紧这些权限)。
一旦您放弃(或确认)问题是由于缺乏适当的凭据引起的,请确保正确转义命令和参数。请参阅此链接:
http://es2.php.net/manual/en /function.exec.php#101579
您还可以尝试的一件事是在调用 exec() 之前关闭 PHP 会话:
http://es2.php.net/manual/en/function.exec.php#99781
The physical directory where your Windows desktop is stored belongs to your user profile folder. That means that other users (including the one Apache runs as, which is typical "Local System") won't have the appropriate permissions to read and write files on it. While you can adjust your Apache set-up to make it run with your own user,
Farzad
, it's more common to put web applications in an entirely different directory tree. It may happen that ConvertPPT.exe just stalls because it's trying to write a file at a location where it's not allowed. I suggest you create a top folder directory and make sure it's world-writeable (once finished, you can tighten these permissions if you like).Once you discard (or confirm) that the issue is caused by lack of appropriate credentials, make sure you are escaping your command and arguments properly. See this link:
http://es2.php.net/manual/en/function.exec.php#101579
One more thing you can try is to close PHP sessions before issuing the call to exec():
http://es2.php.net/manual/en/function.exec.php#99781
您可能遇到了这个错误: http://bugs.php.net/bug.php ?id=44994
这在 PHP 5.3.5 上一直困扰着我很多年,甚至在今天也是如此。
程序的错误输出和程序被重定向到写入其 stderr 输出的 apache 错误日志文件句柄之间似乎存在某种死锁,使得程序永远卡住,直到 apache 进程被杀死。
此外,当使用 passthru 或 system 或反引号运算符时,有一个中间“cmd.exe”进程用于在不可见的控制台中运行程序,并且我发现这个 cmd 进程甚至在没有运行程序的情况下就卡住了。
我现在还没有真正的解决方案,而且似乎这个错误虽然被很多人重现,但还没有得到解决。
You have probably run into this bug: http://bugs.php.net/bug.php?id=44994
which has been bothering me for ages, even today, on PHP 5.3.5.
It seems like there is some kind of deadlock between the error output of the program and the apache error log file handle into which the program is redirected to write its stderr output, making the program be stuck for ever until the apache processes are killed.
Also, when using
passthru
, orsystem
, or the backtick operator, there's an intermediary "cmd.exe" process that is used to run the program in an invisible console, and I have seen this cmd process getting stuck without even running the program.I don't really have a solution as of now, and it seems the bug, even though reproduced by many people, hasn't been resolved.