JNo
JNo

Reputation: 457

PHP - Can't open .exe files with exec command

I work with PHP 5.4, IIS 7.5.

If execute a simple command, it works:

<?php
    exec("dir", $r);
    print_r($r);
?>

But if open .exe file, it doesn't work, the page is loading until the php timeout and doesn't open the notepad:

<?php
    exec("notepad.exe", $r);
    print_r($r);
?>

And if execute the notepad's php in command line, it works:

php -f <file>

I think that the problem is with IIS, but I don't know what. Thanks!

UPDATE

I did another test case and doesn't work, the page finishes loading but doesn't delete the task:

<?php
    $r = exec("SCHTASKS.exe /Delete /TN TaskTest /F");
    print_r($r);
?>

The IIS_IUSRS have permission for execute the schtasks.

SOLUTION

Notepad doesn't open because is a interactive program.

For Tasks scheduler, gives read and write permissions to the task folder (C:\Windows\System32\Tasks) to IUSR.

Upvotes: 1

Views: 4119

Answers (2)

Jon
Jon

Reputation: 437386

The problem is that you are instructing exec to gather and return the output of the spawned process and the process must terminate for this to happen. Since Notepad does not terminate immediately PHP is stuck waiting forever (you can test this by running any non-interactive process instead, for example net.exe).

Takeaway: exec and friends are not meant to launch interactive processes.

In any case, exec will spawn a command interpreter which in turn will spawn Notepad. However, due to security features introduced in recent Windows versions, and depending on the user that IIS is running as, these processes will not create visible windows on your current desktop so there will be nothing for you to see. You will be able to verify that they were spawned using Task Manager or another equivalent program.

Upvotes: 1

Rowland Shaw
Rowland Shaw

Reputation: 38130

What makes you think it isn't working?

Be aware that windows services cannot normally interact with the desktop, so it may be the case that notepad is starting, just not anywhere you can see it - and as PHP will wait for it to terminate, and nobody can see it to terminate it, it'll timeout, as you're seeing.

It may also be the case that the user that the web server is running as does not have execute permissions on the folder that notepad is in (assuming it had the relevant path).

Upvotes: 1

Related Questions