Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

windows - php exec() is not executing the command

I have tried to use exec() with 'whoami' to check if it works and I got the result of

nt authoritysystem

Now I need to run a .exe file with parameters from php via exec() function.

I tried this in command prompt and it actually runs the program with given parameters. This is the example command.


NOTE the exe file gets 3 inputs (folder, file_name, report_file_nmae)

> ....some_file.exe folder="C:path_to_folder" param=1.xml report=2.xml

But when I run this command from php file:

exec('....some_file.exe folder="C:path_to_folder" param=1.xml report=2.xml');

nothing is happening. This is the first time I am using exec() function, so I am not familiar with its details. What is wrong?

I tried using:

  • \ instead of
  • escapeshellarg() on the directory
  • added "" around directory folder names

No luck

Addendum:

echo exec($command)  // echos < .... why?

or

exec($command, $output);
print_r($output);        // Array()

I even changed the permission on the file to full control to all users. If I call the program from command prompt, I can see the icon appearing next to clock for a second.

But the same call from php will not even call the program.

Edit

Even exec('notepad.exe'); is not working. Something has to be done with php configurations maybe?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I already said that I was new to exec() function. After doing some more digging, I came upon 2>&1 which needs to be added at the end of command in exec().

Thanks @mattosmat for pointing it out in the comments too. I did not try this at once because you said it is a Linux command, I am on Windows.

So, what I have discovered, the command is actually executing in the back-end. That is why I could not see it actually running, which I was expecting to happen.

For all of you, who had similar problem, my advise is to use that command. It will point out all the errors and also tell you info/details about execution.

exec('some_command 2>&1', $output);
print_r($output);  // to see the response to your command

Thanks for all the help guys, I appreciate it ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...