简体   繁体   English

从PHP文件调用Windows程序(通过命令提示符)

[英]calling Windows program from PHP file (through command-prompt)

I have tried calling a windows program several ways and I have gotten the same result each time. 我尝试过几种方式调用Windows程序,但每次都得到相同的结果。

The program opens up on my machine (without a GUI) but never closes each means that the browser is forever loading. 该程序在我的机器上打开(没有GUI),但是从不关闭,这意味着浏览器将永远加载。

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 ie there aren't any results). (它刚刚启动,即没有任何结果)。

I just want to know the proper way of starting a program with switches through PHP. 我只想知道通过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

If the program never closes, then PHP can't return a value from exec() . 如果程序永不关闭,则PHP无法从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. 它将以Web服务器定义的任何权限执行。

http://php.net/manual/en/function.exec.php http://php.net/manual/en/function.exec.php

You might consider the advanced functionality of proc_open() . 您可能会考虑proc_open()的高级功能。 It will give you access to all the necessary pipes, but I don't think that will help you in this situation. 它可以让您访问所有必需的管道,但是在这种情况下,我认为这不会对您有所帮助。

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. 如果Windows计算机上的目标目录是C:\\ Program Files(x86)\\ Softinterface,Inc \\ Convert PowerPoint \\ ConvertPPT.exe ,则需要双引号引用其中包含空格字符的目录。

To translate it into php terms, it should be like this: 要将其翻译成php术语,应该是这样的:

$directory = 'C:\"Program Files (x86)"\"Softinterface, Inc"\"Convert PowerPoint"\ConvertPPT.exe';

$command = $directory . ' enter your arguments here';

exec($command, $output, $return_var);

// if $return_var == 0, you hit the jackpot.

The physical directory where your Windows desktop is stored belongs to your user profile folder. Windows桌面存储的物理目录属于用户配置文件文件夹。 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. 这意味着其他用户(包括一个运行Apache的用户,这是典型的“本地系统”)将没有适当的权限来在其上读写文件。 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. 虽然可以调整Apache设置以使其与您自己的用户Farzad一起运行,但将Web应用程序放置在完全不同的目录树中更为常见。 It may happen that ConvertPPT.exe just stalls because it's trying to write a file at a location where it's not allowed. 可能发生ConvertPPT.exe停顿是因为它试图在不允许的位置写入文件。 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 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(): 您可以尝试的另一件事是在发出对exec()的调用之前关闭PHP会话:

http://es2.php.net/manual/en/function.exec.php#99781 http://es2.php.net/manual/en/function.exec.php#99781

You have probably run into this bug: http://bugs.php.net/bug.php?id=44994 您可能已遇到此错误: http : //bugs.php.net/bug.php?id=44994

which has been bothering me for ages, even today, on PHP 5.3.5. 直到今天,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. 程序的错误输出与apache错误日志文件句柄之间似乎存在某种死锁,程序被重定向到该错误中以写入其stderr输出,从而使该程序永远卡住,直到apache进程被杀死为止。

Also, when using passthru , or system , 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. 另外,当使用passthrusystem或backtick运算符时,存在一个中间“ cmd.exe”进程,该进程用于在不可见的控制台中运行该程序,而且我已经看到此cmd进程陷入了困境,甚至没有运行该程序。

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. 到目前为止,我还没有真正的解决方案,尽管很多人都复制了该错误,但该错误似乎尚未解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM