2

I want to open a Windows program (e.g. notepad.exe) when I click a link on website. I tried using the php commands shell_exec() and exec(), but both will not open the Windows program. This is the code that does not work:

$command = "c:\\windows\\system32\\notepad.exe";
echo shell_exec("$command");

In the past I have been using a similar code for making a backup of a database:

$command = "d:\\xampp\mysql\bin\mysqldump --opt -h $hostname -u $username $db > $backup_file";
echo shell_exec("$command");

This code works as expected (the backup is created).

I suspect that the mysqldump is running in the background while notepad.exe is not. Is there a solution for this issue?

Jos R
  • 21
  • 3
  • 2
    Are you running this from a web page, or command line? If from a web page, the webserver has no graphical session so it probably won't open a GUI application. Or if it does, it'll open on the webserver rather than on the machine where your browser is (unless they happen to be the same). If you're trying to get a web page to open a local application on the client machine, there are ways to do that but it requires some setup in the browser first (e.g. you have to set a custom protocol in the way you might have seen with programs such as Zoom, Teams etc). – ADyson Sep 09 '22 at 12:08
  • 1
    All backslashes should be escaped but that likely isn't current issue. Also no reason to quote a variable just do `echo shell_exec($command);` that too isn't a real issue though – user3783243 Sep 09 '22 at 12:24
  • As mentioned by ADyson, GUI applications can not reflect into the browser, unless they have been written that way. On Windows, you can use Thinfinity VirtualUI to write dual mode applications. It requires their server to be running and it's licensed per user. – Rohit Gupta Sep 09 '22 at 14:34
  • @RohitGupta I don't want the external program (e.g. notepad) to run in the browser. I just want it to start as a separate (stand alone) program. – Jos R Sep 09 '22 at 14:40
  • You still can't do that either, for the reasons I already explained - or at least not using this approach. https://stackoverflow.com/questions/32273424/how-to-run-desktop-app-from-browser gets you started on the alternative approach, at least for Windows. The scheme for other OSes is similar too, I believe. – ADyson Sep 09 '22 at 15:02

1 Answers1

0

PHP normally runs as a service. In this mode, under windows, things are restricted. Since a couple of years ago, not even a console is hooked up. Desktop Interaction is not possible.

Anything launched from a service will have the same restrictions.

Above is from my experience. It is corroborated by @Goat in PHP - Any chance to run GUI program through exec()?

Further

Important

Services cannot directly interact with a user as of Windows Vista. Therefore, the techniques mentioned in the section titled Using an Interactive Service should not be used in new code.

MS Docs on Interactive Services

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41