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?

5
  • 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). Commented Sep 9, 2022 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 Commented Sep 9, 2022 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. Commented Sep 9, 2022 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. Commented Sep 9, 2022 at 14:40
  • You still can't do that either, for the reasons I already explained - or at least not using this approach. stackoverflow.com/questions/32273424/… gets you started on the alternative approach, at least for Windows. The scheme for other OSes is similar too, I believe. Commented Sep 9, 2022 at 15:02

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.