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?

jww
  • 97,681
  • 90
  • 411
  • 885
Brian
  • 4,958
  • 8
  • 40
  • 56
  • 1
    Make sure PHP has permission to excecute that file. – Joren Jul 29 '13 at 00:49
  • how can i make sure..... – Brian Jul 29 '13 at 00:54
  • I honestly do not know how to do that in Windows since it's permission system is real inconsistent. – Joren Jul 29 '13 at 01:06
  • what's the `..\..\some_dir` in the beginning for ? – Nir Alfasi Jul 29 '13 at 01:27
  • 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. – mattosmat Jul 29 '13 at 01:37
  • Yes, it gives me `<`....why? – Brian Jul 29 '13 at 02:05
  • @alfasin did not get you question – Brian Jul 29 '13 at 02:08
  • You need the complete output, not only the last line. See: http://php.net/manual/en/function.exec.php ... This means you have to have an array like in my example. – mattosmat Jul 29 '13 at 02:19
  • 1
    @A.S.Roma my question is regarding the syntax of the command: it looks like: `relative-path-to-folderfull-path\file.exe...` and I don't get what the first part is for - how does it work from command-prompt ? – Nir Alfasi Jul 29 '13 at 02:28
  • Oh, I see, that was a typo, I EDITED...tnx – Brian Jul 29 '13 at 02:31
  • Break this problem down a bit. If you specify the full path to the EXE, does it work? If you allow any user on the system to execute, does it work? – Brad Jul 29 '13 at 02:35
  • but if from command prompt it works without full path, do you thing that is the reason? Because the .exe is relative from that path to php file that calls the exec()... What do you want me to add...just tell me – Brian Jul 29 '13 at 02:40
  • @Brad No, full path did not change anything. – Brian Jul 29 '13 at 02:43
  • Yes, that you could be the reason because the server user executes from another folder! – mattosmat Jul 29 '13 at 02:43

2 Answers2

119

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

Brian
  • 4,958
  • 8
  • 40
  • 56
  • 6
    Knowing this helped a lot. Couldn't figure out why I wasn't getting my response back. Thanks. – earl3s Apr 15 '15 at 21:10
  • 1
    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. – Dipu Raj Jun 03 '15 at 10:22
  • If you're running XAMPP checkout this: http://stackoverflow.com/questions/24941078/ffmpeg-and-php-on-mac-doest-work – Ratata Tata Feb 21 '16 at 23:21
  • 2
    Adding 2>&1 helped me as well. can someone explain what does it mean? – Rakesh K Mar 18 '16 at 09:26
  • @DipuRaj I have the same problem, where did you added those permissions? directly on the server or in PHP? I need your help please. – Professor Zoom Nov 22 '16 at 21:08
  • 2
    @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. – aagjalpankaj Jul 21 '17 at 06:25
  • i am getting error "perable program or batch file" while i use above syntax even simple command ipconfig too. – Balaji Nov 16 '19 at 10:58
  • @ßãlãjî what is your PHP version? And can you write the PHP code here exactly the way you do to get the message? – Brian Nov 18 '19 at 15:36
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.

craned
  • 2,991
  • 2
  • 34
  • 38