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

Issue

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;
}

Solution

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.

Answered By – dev-null-dweller

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published