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.

4
  • 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? Commented Nov 25, 2010 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. Commented Nov 25, 2010 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. Commented Nov 25, 2010 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 if the program is not working , share the code to check it togther. if the problem is with arguments , here is a good explanation. Commented Jun 22, 2015 at 7:58

5 Answers 5

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

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

1 Comment

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'}");

1 Comment

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
3

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

Comments

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.

1 Comment

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.......
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.