編輯:幾期試驗研究,我看到執行DIR命令的唯一方法之後是這樣的:
cmd.exe /c dir c:\
所以,你可以使用try我的解決方案:
$command = 'cmd.exe /c dir c:\\';
您可以使用proc_open函數,它可以讓你訪問stderr,返回狀態碼和stdout。
$stdout = $stderr = $status = null;
$descriptorspec = array(
1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
2 => array('pipe', 'w') // stderr is a pipe that the child will write to
);
$process = proc_open($command, $descriptorspec, $pipes);
if (is_resource($process)) {
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// 2 => readable handle connected to child stderr
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$status = proc_close($process);
}
return array($status, $stdout, $stderr);
有趣的部分與proc_open,比起像POPEN或者exec等功能,是它提供了具體的選項,Windows平臺,如:
suppress_errors (windows only): suppresses errors generated by this function when it's set to TRUE
bypass_shell (windows only): bypass cmd.exe shell when set to TRUE
也許是因爲有錯誤? :)檢查什麼錯誤1代碼的意思... – Catalin 2012-02-02 15:05:49
如果我啓動相同的命令CMD shell與PHP script.php它的作品,當我從瀏覽器(所以網絡服務器)啓動它不起作用,我真的不明白其中錯誤是 – albanx 2012-02-02 15:10:24
@albanx:這會讓我相信權限,如果它運行時,而不是當IIS運行時。 – 2012-02-02 15:19:08