2010-12-07 143 views
0

我試圖從命令行使用PHP運行.bat文件。我正在使用Windows Vista家庭高級版。PHP +命令行執行

當我在像ipconfig.exe這樣的文件上使用腳本時,我得到輸出。但是,當我運行.bat文件時,它會輸出文件中的內容,但不會執行它。

下面是什麼作品,並給我輸出:

$runCommand = "C:\\WINDOWS\\system32\\ipconfig.exe"; 
$WshShell = new COM("WScript.Shell"); 
$output = $WshShell->Exec($runCommand)->StdOut->ReadAll; 
echo "<p>$output</p>"; 

但這並不:

$runCommand = "C:\\Temp\\foo.bat"; 
$WshShell = new COM("WScript.Shell"); 
$output = $WshShell->Exec($runCommand)->StdOut->ReadAll; 
echo "<p>$output</p>"; 

下面是在我的foo.bat文件:

C:/windows/system32/schtasks.exe /create /tn "TestTask" /tr "C:/Temp/configure.php" /sc minute /st 08:00:00 

如果我複製這個並粘貼到我的Windows命令行中,此命令執行成功。

不知道發生了什麼事。請協助。

+1

12的重複問題,1接受答案。請閱讀FAQ:http://stackoverflow.com/faq – 2010-12-07 18:25:55

回答

1

這是因爲bat文件是用於提示的排隊列表的命令。嘗試以下方法:

cmd /c myfile.bat

(它可能是/ K太,忘記其執行和關閉)

此外,How do you run a .bat file from PHP?

EDIT

<?php 
    // http://www.php.net/manual/en/function.exec.php#85930 

    $_ = null; 

    // If you care about the return value, use this: 
    passthru("C:\\WINDOWS\\system32\\cmd.exe /c custom.bat",$_); 
    header('Content-Type: text/plain'); 
    echo $_; 
    // if you don't care, just use this: 
    $_ = exec("C:\\WINDOWS\\system32\\cmd.exe /c custom.bat"); 
?> 
+0

我將C:\\ Temp \\ foo.bat替換爲C:\\ WINDOWS \\ system32 \\ cmd.exe/k C:\\ Temp \\ foo.bat但它仍然不會執行。 – 2010-12-07 18:35:13