windows php 如何启动外部程序运行 - 系统和执行有问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1403203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:06:33  来源:igfitidea点击:

php How do I start an external program running - Having trouble with system and exec

phpwindows

提问by sdfor

I'm running in a controlled, xp, intranet only environment and I need to start external processes from a PHP applications. (Backups, Reports etc.)

我在一个受控的、xp 的、仅限 Intranet 的环境中运行,我需要从 PHP 应用程序启动外部进程。(备份、报告等)

I can get system or exec to start processes that work silently. Here's a simple example

我可以让 system 或 exec 启动静默运行的进程。这是一个简单的例子

<?php exec ("echo hello > hello.txt");?> 

I can get it to execute a bat file that has no visible output.

我可以让它执行一个没有可见输出的bat文件。

I can't get any program that has a screen to run such as a report generator or notepad...

我无法运行任何具有屏幕的程序,例如报告生成器或记事本...

<?php exec ("explorer");?>

doesn't do anything. or same for system

什么都不做。或系统相同

采纳答案by Nathan

What behavior are you expecting? Calling system('notepad')works fine - it just doesn't display the GUI. It runs in the background, and PHP sits there patiently waiting for notepad to close itself (and only continues if you kill notepad from the process list).

你期待什么行为?调用system('notepad')工作正常 - 它只是不显示 GUI。它在后台运行,PHP 耐心地等待记事本关闭(并且只有在您从进程列表中杀死记事本时才会继续)。

If you're expecting it to pop up a GUI, I'm fairly certain that you can't do that. ;) An option might be to write out a batch script (file_put_contents('runme.bat', 'notepad hello.txt')) and have that batch script queued (with Windows scheduler or whatever the cron-equivalent is on Windows) to run in an async fashion (and clear itself at the end).

如果您希望它弹出一个 GUI,我很确定您不能那样做。;) 一个选项可能是写出一个批处理脚本 ( file_put_contents('runme.bat', 'notepad hello.txt')) 并将该批处理脚本排队(使用 Windows 调度程序或 Windows 上的任何等效 cron)以异步方式运行(并在最后清除自身)。

回答by Chris Baker

Very late answer, but I was working on this myself and found that it is indeed possible to run a GUI program from PHP with the Apache server on Windows XP.

很晚的答案,但我自己正在研究这个,发现确实可以在 Windows XP 上使用 Apache 服务器从 PHP 运行 GUI 程序。

  • Start->Run, type "services.msc" to bring up Services control (other ways to get there, this is easiest IMO)
  • Locate your Apache service (mine was called "wampapache" using WampServer 2.0)
  • Open the service properties (double-click or right click->properties)
  • Flip to the Log On account and ensure the checkbox titled "Allow service to interact with Desktop" is checked
  • Flip back to the General tab, stop the service, start the service
  • 开始->运行,输入“services.msc”以调出服务控制(其他方式到达那里,这是最简单的IMO)
  • 找到您的 Apache 服务(我使用 WampServer 2.0 称为“wampapache”)
  • 打开服务属性(双击或右键->属性)
  • 翻转到登录帐户并确保选中了标题为“允许服务与桌面交互”的复选框
  • 翻回常规选项卡,停止服务,启动服务

Now, using the code below, you can spawn UI processes from PHP. In the first code snippet, the script will not wait for the application to close; the second snippet waits for the program to close before continuing (blocking).

现在,使用下面的代码,您可以从 PHP 生成 UI 进程。在第一个代码片段中,脚本不会等待应用程序关闭;第二个片段在继续(阻塞)之前等待程序关闭。

Do not wait for application:

不要等待申请:

pclose(popen("start /B notepad.exe", "r"));

pclose(popen("start /B notepad.exe", "r"));

Wait for application:

等待申请:

system('start notepad.exe');

system('start notepad.exe');

This has been tested on Windows XP. I have not tried it on any other Windows versions, your millage may vary.

这已经在 Windows XP 上进行了测试。我还没有在任何其他 Windows 版本上尝试过,您的体验可能会有所不同。

Side noteOn my particular installation, I was using the other option in the Log In tab of the service - Apache was running as a domain user so it could access several network shares with domain user permissions. The checkbox isn't available for that option, only when the service is running as Local System. After extensive research, I've found that there is simply no way for a single service to both interact with the current desktop AND utilize the credentials of a specific user. It's a one-or-the-other proposition, with the suggested remedy being to split your service into two components - one that uses the user account privs and one that interacts with the desktop. Not very practical when the service you're talking about is the web server. This note is probably pretty specific to my use case, but I wanted to put it out here in case I can save someone else the frustration in the future.

边注在我的特定安装中,我使用了服务的“登录”选项卡中的另一个选项 - Apache 以域用户身份运行,因此它可以使用域用户权限访问多个网络共享。只有当服务作为本地系统运行时,该复选框才可用于该选项。经过广泛的研究,我发现单个服务根本无法与当前桌面交互并利用特定用户的凭据。这是一个二选一的提议,建议的补救措施是将您的服务分成两个组件 - 一个使用用户帐户权限,一个与桌面交互。当您谈论的服务是 Web 服务器时,这不太实用。这个注释可能非常特定于我的用例,

回答by jxmallett

Another super late answer, but this comes up on Google when searching for "php run gui program"...

另一个超级迟到的答案,但在搜索“php run gui program”时出现在谷歌上......

I have been able to launch a GUI app in Windows 8.1 by making, running and deleting a scheduled task:

通过创建、运行和删除计划任务,我已经能够在 Windows 8.1 中启动 GUI 应用程序:

shell_exec('SCHTASKS /F /Create /TN _notepad /TR "notepad.exe" /SC DAILY /RU INTERACTIVE');
shell_exec('SCHTASKS /RUN /TN "_notepad"');
shell_exec('SCHTASKS /DELETE /TN "_notepad" /F');