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

邮差的信 提交于 2019-12-13 04:14:13

问题


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?


回答1:


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.




回答2:


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)




回答3:


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.)



来源:https://stackoverflow.com/questions/16037024/why-cannot-run-an-executable-file-with-exec-or-system-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!