11

I want to run a c++ code in php script. It takes 6 runtime arguments.
I am trying with:

exec("./controller.exe",{"125", "70", "127", "220" ,"0.5", "0.4"});

But it is not working.

António Almeida
  • 9,620
  • 8
  • 59
  • 66
sahil
  • 1,108
  • 3
  • 15
  • 25
  • 2
    What do you mean with c++ code? Is it pure code, or was it compiled? And exactly what isn't working, i.e., what's your error message? – cadolphs Nov 25 '10 at 16:20
  • 2
    Check the PHP documentation for exec. The second argument is not runtime arguments, you need to concat those to the "./controller.exe" string. – Frode Nov 25 '10 at 16:23
  • Also, make sure the C++ application was compiled for the architecture/platform you are running php on. Most windows cli programs will not run in a linux server environment. – Rahly Nov 25 '10 at 17:04
  • can you be more specific ? what is exactly did not work ? is it a program not working at all or it can't read arguments ? and what is your server os ? if your sever based on linux , then you will never be able to run .exe file. see how to compile and run c++ file from this link : [Click Here](http://www.cyberciti.biz/faq/howto-compile-and-run-c-cplusplus-code-in-linux/) if the program is not working , share the code to check it togther. if the problem is with arguments , [here](http://www.cplusplus.com/forum/articles/13355/) is a good explanation. – Abdelazeez Mabrouk Jun 22 '15 at 07:58

5 Answers5

9

You can use the call:

exec("./controller.exe 125 70 127 220 0.5 0.4", $out);

$out will hold the output if you are interested

Khaled
  • 1,114
  • 14
  • 31
  • Here are some more examples: http://webtutsdepot.com/2010/03/21/c-plus-plus-php-and-jquery/ – Firze Aug 12 '14 at 09:23
5

PHP scripts are run by php.exe so unless you have controller.exe in the same folder with php or your folder that contains controller.exe is in your path variable it wont work.

Try giving it absolute path.

The arguments should be passed in the same string as the executable, so something like this:

exec("/c/project/controller.exe {'125', '70', '127', '220' ,'0.5', '0.4'}");
Caner
  • 57,267
  • 35
  • 174
  • 180
  • The problem is that argvs should be passed in the same string as the executable - the second arg is a (optional) reference to an variable to hold the output – symcbean Nov 25 '10 at 16:44
3

You can use PHP's system() to execute things via command line.

2

You can use this sample code:

<?PHP
    $output=shell_exec("controller.exe 125 70 127 220 0.5 0.4");
    echo $output;
?>

It is working very good for me. Place both controller.exe and xx.php in same folder.

vassi
  • 5
  • 4
vasanth kumar
  • 522
  • 1
  • 5
  • 18
  • 1
    Always remember if you think the given solution is correct or helpful then upvote the solution and accept as a correct solution..............make it habit....... – vasanth kumar May 17 '13 at 08:33
2

To make your C++ code run on PHP you either specify the path of the code or put that code in the PHP folder. Then follow this command:

exec("/c/project/controller.exe {'125', '70', '127', '220' ,'0.5', '0.4'}");

To hold the output you can include another argument $output after curly braces. and print that output.

Rajkamal Mishra
  • 121
  • 1
  • 9