问题
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;
}
回答1:
Few advices that may help:
- Use
/instead, it also work under windows. - If your path contain spaces, wrap it in double quotes
$exec = '"C:/my path/file.exe"'; - Parameters should be passed outside double quotes
$exec = '"C:/my path/file.exe" /help'; - Make sure that your program actually writes to STDOUT, not STDERR.
回答2:
In a single-quoted string, you still need to escape backslashes, so to get \ you need \\:
exec('C:\\path\\file.exe',$out);
回答3:
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.
回答4:
Check your config/php.ini file, exec function could be disbaled under disable_functions or check the line open_basedir, PHP could be limited to access certain directories
回答5:
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);
?>
回答6:
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
回答7:
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
来源:https://stackoverflow.com/questions/17458610/cant-run-exe-files-using-exec-in-php