我在Windows10環境中運行。我的php腳本寫了一個pdf文件,我只是想在我指定的pdf查看器中打開文件。我正在使用PHP手冊中給出的「runAsynchronously」函數,並且我嘗試了很多變體。我沒有問題讓進程在後臺運行 - 它每次出現在我的TaskManager進程列表中,但沒有出現窗口 - 我做錯了什麼?如果我雙擊已寫入的鏈接文件,它可以正常工作。這與可執行文件或文件名的路徑無關 - 我可以用「notepad.exe」替換pdf查看器,並用合適的文本文件替換$文件 - 同樣的事情發生,記事本顯示爲一個進程,但不是作爲一個窗口,並且鏈接工作正常。從PHP開始外部程序
下面是一些代碼片段
$cmd = "C:\\Program Files (x86)\\SumatraPDF\\SumatraPDF.exe";
runAsynchronously($cmd, $file, 7, null, true);
function runAsynchronously($path, $arguments, $windowstyle=1, $lnkfile=null, $exec=true) {
$tmp = (is_null($lnkfile)) ? 'C:\temp\temp.lnk' : $lnkfile;
try {
if(file_exists($tmp)) { unlink($tmp); }
$WshShell = new COM("WScript.Shell");
$oShellLink = $WshShell->CreateShortcut($tmp);
$oShellLink->TargetPath = $path;
$oShellLink->Arguments = $arguments;
$oShellLink->WorkingDirectory = dirname($path);
$oShellLink->WindowStyle = 1;
$oShellLink->Save();
$waitforcompletion = false;
if($exec) {
// Run kicks off the process in the background, but no window gets opened
$oExec = $WshShell->Run($tmp, $windowstyle, $waitforcompletion);
unlink($tmp);
} // if not executed link is left available for manual running
unset($WshShell,$oShellLink,$oExec);
} catch(Exception $ex) {
print $ex->getMessage();
}
}
你知道嗎PHP的意思是服務器端腳本?恕我直言,這是針對不同語言的程序 –
@JozefDochan [「PHP是一種流行的通用腳本語言,特別適合web開發。」](http://www.php.net)。 「通用」是那裏的關鍵詞。 – bishop
可能重複[php如何啓動一個外部程序運行 - 遇到系統和可執行文件](http://stackoverflow.com/questions/1403203/php-how-do-i-start-an-external-program -running-having-trouble-with-system-and) – HPierce