enenen
enenen

Reputation: 1967

Why cannot run an executable file with exec() or system() functions?

I'm trying to run notepad on the server (localhost for now).

exec() and system() functions are working fine when for example write ping 127.0.0.1.

But this does not work (working fine if I write the command directly in the command prompt):

$command = "C:\WINDOWS\system32\notepad.exe";

$result = system($command);

print_r($result);

Using Windows XP with xampp. Probably I don't have permissions because the command is executed from some other account but I don't know how to check this.

Any advices?

Edit:

As bwoebi said, I have opened processes but they are opened from a different user (SYSTEM) and I can't see when the application is opened. So, I have to paraphrase my question: how to change the user which is used when executing commands from a PHP script?

Upvotes: 1

Views: 2722

Answers (3)

zakinster
zakinster

Reputation: 10698

First you need to escape the backslashes in your command string if you're not using single quotes :

$command = "C:\\WINDOWS\\system32\\notepad.exe";

Also note that if Apache is running as a Windows service, it does not have desktop interaction permission, so it can't open a GUI, try running the script directly with PHP on the command line.

EDIT

The user used to run command is the user that is running PHP. To change the user running PHP, you'll have to change the user running Apache, if you want this user to have desktop interaction permission, you'll have to run Apache yourself and not as a service.

Upvotes: 2

Dave
Dave

Reputation: 3288

Note pad is a GUI program so requires the windows TTY to be active.

Ping is command line so can be ran by the system directly and piped results into the program calling it.

With out getting into too much detail of how os's work basically it can't be done on a windows machine (its possible on unix machines but more difficult.)

Upvotes: 0

bwoebi
bwoebi

Reputation: 23787

let the process a bit sleep after executing the shell command and search in the TaskManager for a Notepad... Then you'll see that this are two different users (and you don't see the other users Notepad)

Upvotes: 0

Related Questions