47

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

nt authority\system

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?

14
  • 1
    Make sure PHP has permission to excecute that file. Commented Jul 29, 2013 at 0:49
  • how can i make sure..... Commented Jul 29, 2013 at 0:54
  • I honestly do not know how to do that in Windows since it's permission system is real inconsistent. Commented Jul 29, 2013 at 1:06
  • 6
    Have you tried getting the output from exec()? Try something like $out = array(); exec('your_command 2>&1',$out);; although the 2>&1 is a linux thing. Commented Jul 29, 2013 at 1:37
  • 1
    @A.S.Roma my question is regarding the syntax of the command: it looks like: relative-path-to-folder<space>full-path\file.exe... and I don't get what the first part is for - how does it work from command-prompt ? Commented Jul 29, 2013 at 2:28

2 Answers 2

121

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

Sign up to request clarification or add additional context in comments.

8 Comments

Knowing this helped a lot. Couldn't figure out why I wasn't getting my response back. Thanks.
Yes, it helped a lot. and I got a result as "Access is Denied" on PHP on IIS. So I have added permission for NEWTWORK USER to the file I was executing.
If you're running XAMPP checkout this: stackoverflow.com/questions/24941078/…
Adding 2>&1 helped me as well. can someone explain what does it mean?
@RakeshK - The '2>&1' is for redirecting errors to the standard IO. And the most important thing is the '&' at the end of the command string, which tells the terminal not to wait for a response.
|
0

You might also try giving the full path to the binary you're trying to run. That solved my problem when trying to use ImageMagick.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.