14

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

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

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

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

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

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
sdfor
  • 6,324
  • 13
  • 51
  • 61

3 Answers3

19

Another super late answer, but this comes up on Google when searching for "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:

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');
jxmallett
  • 4,087
  • 1
  • 28
  • 35
  • I have windows 7 and tried other answers while failed to start gui but this one is working perfect. – Mojtaba Rezaeian Mar 17 '16 at 12:10
  • also been battling on Win10 with none of the solutions working - this works 100% +1 for outthebox.. – l0ft13 Mar 17 '16 at 19:13
  • How to open local folders i.e. file system? – RN Kushwaha Mar 19 '16 at 19:14
  • Thanks a ton. It worked for my AWS Windows server 2012 instance. I was looking for this solution past 2 days. – Amol Bharmoria Apr 05 '16 at 15:59
  • Works beautifully in Windows 7. Switched argument to cmd.exe /c {Path-to-blah} and viola. Works perfect. – Bren1818 Mar 28 '17 at 13:08
  • i have to grant administrator right to cmd in win8 to make this works. thank you very much sir, have a nice day :) – Baim Wrong Jul 24 '17 at 02:53
  • I love you! :) tried it on windows 10 with chrome and works perfect! Thank you! – G. Threepwood Jun 19 '18 at 15:57
  • This works good on Windows 10, but on a Windows Server 2019 worked only after changing manually the settings of the task to allow multiple parallel instances. This means, it is / was not possible to create, run and delete the task on the fly... – Billy G May 12 '21 at 08:33
  • On a Windows Server 2019, when started this way, my application (putty.exe) still exists / runs in the background (as showing in task manager) after closing it. – Billy G May 12 '21 at 08:36
19

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.

  • 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

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).

Do not wait for application:

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

Wait for application:

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.

Side note On 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.

Chris Baker
  • 49,926
  • 12
  • 96
  • 115
  • 2
    I'm curious as to how this got 10 upvotes--it still only runs notepad as a background process for my setup even though I have it on "interact with desktop". I'm using Windows 7. – user24601 Feb 21 '14 at 16:55
  • @user24601 This has only been tested for Windows XP. I will make a note of that in the answer. As for the upvotes... who cares? – Chris Baker Feb 21 '14 at 17:15
  • Working fine on Windows 7 – mpyw Jun 18 '14 at 02:04
  • Clearly this solution only works in a limited number of environments. I have Windows 7 and it does not work, nor have I found anyone else except for @CertaiN who says that it does work. – Sheldon Juncker Feb 01 '15 at 17:50
6

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).

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).

Nathan
  • 314
  • 1
  • 3
  • I see what you mean. You are right notepad is running without a GUI. (My configuration timed out after 60 seconds.) So I can't start a GUI based program out of PHP and have to resort to OS based solutions. It's good to know. thanks ! – sdfor Sep 10 '09 at 04:01