Running Windows program from within PHP

Viewed 36

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?

1 Answers

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

Related