Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php exec() is not executing the command

Tags:

php

windows

exec

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?

like image 832
Brian Avatar asked Jul 29 '13 00:07

Brian


People also ask

How do I know if php exec is enabled?

php phpinfo(); ?> You can search for disable_functions and if exec is listed it means it is disabled. To enable it just remove the exec from the line and then you need to restart Apache and you will be good to go. If exec is not listed in the disable_functions line it means that it is enabled.

What is php exec function?

The exec() function is an inbuilt function in PHP which is used to execute an external program and returns the last line of the output. It also returns NULL if no command run properly. Syntax: string exec( $command, $output, $return_var )

How do I run a php command in terminal?

You can execute linux commands within a php script - all you have to do is put the command line in brackits (`). And also concentrate on exec() , this and shell_exec() ..

Does php exec wait until finished?

PHP exec will wait until the execution of the called program is finished, before processing the next line, unless you use & at the end of the string to run the program in background.


1 Answers

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

like image 111
Brian Avatar answered Sep 21 '22 13:09

Brian