[英]Can't run .exe files using exec() in PHP
我正在尝试使用 .exe 文件来执行计算并将输出传递到 PHP。 我使用 C++ 制作了一个 Hello World .exe 文件,但我无法让 PHP 执行它。
如果我从 CMD 运行这个命令,我会得到正确的输出:
C:\path\file.exe
但是如果我在 PHP 中这样做,输出是一个空字符串:
exec('C:\path\file.exe',$out);
var_dump($out);
但这会显示正确的输出:
exec('ipconfig',$out);
var_dump($out);
我在 Windows 7 上使用 WAMP。
编辑:这是 C++ 程序:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World" << endl;
return 0;
}
很少有建议可能会有所帮助:
/
代替,它也可以在Windows下工作。 $exec = '"C:/my path/file.exe"';
$exec = '"C:/my path/file.exe" /help';
在单引号字符串中,您仍然需要转义反斜杠,因此要获得\\
需要\\\\
:
exec('C:\\path\\file.exe',$out);
检查你的config / php.ini文件,exec函数可以在disable_functions下解析或检查open_basedir行,PHP可以限制访问某些目录
使用return_var参数检查命令的返回值。
$return = -1;
exec('C:\path\file.exe',$out,$return);
echo "Return value: $return\n";
var_dump($out);
在你的情况下,如果它成功执行它应该返回0.如果找不到文件,它可能会返回1.如果我的怀疑是正确的,我认为最可能的返回值是-1073741515。
错误-1073741515(0xc0000135)是您的应用程序缺少DLL时返回的内容。 如果您使用编译器的运行时库的DLL版本,则会发生这种情况。
在本地运行时,应用程序可以正常工作,您可以在其中安装DLL,但在从Web服务器运行时可能仍然会失败,这不一定会有。
如果这是问题,您需要重新编译应用程序以使用静态库,或者在Web服务器上安装必要的DLL。 您没有说明您使用的是哪种编译器,但有关Visual C ++使用的DLL的更多信息,请参阅此处 。
我已经复制了你的代码来测试它并且:
我第一次没有输出。
我添加了file_exists测试并得到:
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
我将file.exe移动到test.php的同一目录并获取:
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);
?>
这应该工作:
exec('"C:\\folder name with space\\program.exe" argument1 "argument2 with space"', $output, $return);
var_dump($output); //"Hello World"
var_dump($return); //0
我遇到过同样的问题。 您可以创建一个test.bat
文件并在其中输入( fwrite()
)命令。 然后执行( exec('test.bat', $out, $ret)
)bat文件。
test.bat
包含:
C:\path\file.exe
$return = -1;
exec('C:\path\file.exe',$out,$return);
echo "Return value: $return\n";
var_dump($out);
和输出是这样的:
Return value: 2 array(0) { }
我使用这个条件,见下文:
if (file_exists($file)) {
echo "The file $file exists";
} else {
echo "The file $file does not exist";
}
输出是:文件 ./filenema 存在”
但是我不能在 php 中使用 exec 命令,知道有什么问题吗???
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.