Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't run .exe files using exec() in PHP

Tags:

c++

php

exe

I'm trying to use an .exe file to perform calculations and pass the output into PHP. I made a Hello World .exe file using C++, but I can't get PHP to execute it.

If I run this command from the CMD, I get the correct output:

C:\path\file.exe

But if I do this in PHP, the output is an empty string:

exec('C:\path\file.exe',$out);
var_dump($out);

But this displays the correct output:

exec('ipconfig',$out);
var_dump($out);

I'm using WAMP on Windows 7.

Edit: Here is the C++ program:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello World" << endl;
    return 0;
}
like image 365
Leo Jiang Avatar asked Dec 01 '22 03:12

Leo Jiang


1 Answers

Few advices that may help:

  1. Use / instead, it also work under windows.
  2. If your path contain spaces, wrap it in double quotes $exec = '"C:/my path/file.exe"';
  3. Parameters should be passed outside double quotes $exec = '"C:/my path/file.exe" /help';
  4. Make sure that your program actually writes to STDOUT, not STDERR.
like image 54
dev-null-dweller Avatar answered Dec 04 '22 04:12

dev-null-dweller