[英]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.