0

I want to run a PHP script which calls an executable cpp file on a remote server.

I have tried like below:

1.Created a cpp file

// function example
#include <iostream>
using namespace std;

int addition (int a, int b)
{
    int r;
    r=a+b;
    return r;
}

int main ()
{
    int z;
    z = addition (5,3);
    cout << "The result is " << z;
    return z;
} 

Generated its .exe file and put it in server's folder(test.exe)

Step2 : Created a php scripts which call exe file using 'shell_exec'

<?php 
if (function_exists('shell_exec')){
    echo "Enabled";
} else {
    echo "Disabled";
}
$file = 'test.exe';
if (!file_exists($file)) echo 'File does not exists';
$out= shell_exec($file);
//exec($file, $out);
echo 'ouput is:: ' .$out;?>

Also, I've put this PHP file on a remote server and tried to call the PHP script in the browser. But it shows error Warning: shell_exec() [function.shell-exec]: Unable to execute 'test.exe'. I want to echo "ouput is:: 8".

Please help to verify.

9
  • 1
    stackoverflow.com/questions/11789095/run-c-script-in-php Commented Jul 17, 2015 at 7:24
  • 1
    It is not question about C++. Your program can be written in any language. Commented Jul 17, 2015 at 7:25
  • 1
    is remoteserver a windows server? has your php-user rights to execute the programm-file? Commented Jul 17, 2015 at 7:27
  • 1
    Most likely your remote server is a Linux machine, which can't understand exe executables. You have to compile your program explicitly for server architecture. Commented Jul 17, 2015 at 7:27
  • 1
    yes remote server is windows server Commented Jul 17, 2015 at 7:27

1 Answer 1

1

Use:

exec("./test.exe", $out);

Note that $out will hold the output.
remember thatshell_exec returns all of the output stream as a string, but exec returns the last line of the output.

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

3 Comments

it shows Warning: exec() [function.exec]: Unable to fork [./test.exe] in D:\Hosting\10676289\html\cpp\test.php on line 11 ouput is:: Array
Have you googled the error? I've googled it for you & this is what i came across:stackoverflow.com/questions/20648949/…
tried as in above link , echo system("ulimit -a"); shows Unable to fork

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.