Search code examples
phpc++exec

php exec() not showing expected output


I am trying to run cpp executable on php, but its not working. Please help.

 var_dump(shell_exec("D:\\c_cpp_programs\\string.exe"));
 $output = system('D:\\c_cpp_programs\\string.exe', $retval);
 var_dump($output);
 passthru ('D:\\c_cpp_programs\\string.exe');
 if(file_exists('D:/c_cpp_programs/string.exe'))
    echo "File exist";
 exec('D:/c_cpp_programs/string.exe',$array,$error);
 var_dump($array);
 var_dump($error);

below is the output I get,

C:\wamp64\www\chatclub\application\views\test.php:2:null
C:\wamp64\www\chatclub\application\views\test.php:4:string '' (length=0)
File exist
C:\wamp64\www\chatclub\application\views\test.php:9:
array (size=0)
  empty
C:\wamp64\www\chatclub\application\views\test.php:10:int -1073741515

cpp program is,

#include<iostream>
using namespace std;

int main(){   
    cout<<"Hello"<<endl;
    return 0;
}

Gives me the output on PHP Interactive shell though,

C:\Users\Shoyeb>php -a
Interactive shell

php > var_dump(shell_exec("D:\\c_cpp_programs\\string.exe"));
string(5) "Hello"
php > var_dump(shell_exec("D:\\c_cpp_programs\\string.exe"));
string(6) "Hello
" <----- this output is with <<endl
php > var_dump(shell_exec("D:\\c_cpp_programs\\string.exe"));
string(5) "Hello"
php >

 

Solution

  • Finally I got the output when I started wamp with admin privileges,

    C:\wamp64\www\chatclub\application\views\test.php:2:string 'Hello
    ' (length=6)
    Hello
    C:\wamp64\www\chatclub\application\views\test.php:4:string 'Hello' (length=5)
    Hello File exist
    C:\wamp64\www\chatclub\application\views\test.php:9:
    array (size=1)
      0 => string 'Hello' (length=5)
    C:\wamp64\www\chatclub\application\views\test.php:10:int 0