简体   繁体   中英

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

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

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.

在单引号字符串中,您仍然需要转义反斜杠,因此要获得\\需要\\\\

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

检查你的config / php.ini文件,exec函数可以在disable_functions解析或检查open_basedir行,PHP可以限制访问某些目录

Use the return_var parameter to check the return value from the command.

$return = -1;
exec('C:\path\file.exe',$out,$return);
echo "Return value: $return\n";
var_dump($out);

In your case, if it was executed successfully it should return 0. If the file was not found, it will probably return 1. If my suspicions are correct, though, I think the most likely return value is -1073741515.

Error -1073741515 (0xc0000135) is what is returned when your application has missing DLLs. This would happen if you are using the DLL version of your compiler's run-time library.

The app may work OK when run locally, where you have the DLLs installed, but might still fail when run from your web server, which won't necessarily have them.

If this is the problem, either you need to recompile your app to use static libraries, or install the necessary DLLs on the web server. You don't say what compiler you are using, but for more information on the DLLs used by Visual C++, see here .

I have copied your code to test it and:

The first time I get not output.

I added the file_exists test and get:

Warning: file_exists(): open_basedir restriction in effect. 
File(xxxxxxxxx) is not within the allowed path(s): (xxxxx:xxxx:xxxx:xxxx:xxx:xxx:) 
in xxxxxx/test.php on line 4 
Call Stack: 0.0004 645640 1. {main}() xxxx/test.php:0 0.0004 646016 2. 
file_exists() xxxxxx/test.php:4

I moved file.exe to the same directory of test.php and get:

array(1) { [0]=> string(11) "Hello World" }

PHP:

<?php
$file = 'xxxxx/file.exe';
if (!file_exists($file)) echo 'File does not exists';
exec($file, $out);
var_dump($out);
?>

This should work:

exec('"C:\\folder name with space\\program.exe" argument1 "argument2 with space"', $output, $return);
var_dump($output); //"Hello World"
var_dump($return); //0

I had the same issue. You can create a test.bat file and enter( fwrite() ) the command in it. Then execute( exec('test.bat', $out, $ret) ) that bat file.

test.bat contains:

C:\path\file.exe
   $return = -1;
   exec('C:\path\file.exe',$out,$return);
   echo "Return value: $return\n";
   var_dump($out);

and output is like:

 Return value: 2 array(0) { } 

I use this conditional, see below:

 if (file_exists($file)) {
      echo "The file $file exists";
   } else {
      echo "The file $file does not exist";
   }

Output is: the file ./filenema exists"

But I can not use exec command in php, any idea what is wrong ???

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM