<p>编辑:经过几次研究,我看到执行DIR命令的唯一方法是:</p>
<pre><code>cmd.exe /c dir c:\
</code></pre>
<p>因此您可以使用以下方法尝试我的解决方案:</p>
<pre><code>$command = 'cmd.exe /c dir c:\\';
</code></pre>
<p>您也可以使用proc_open函数,该函数允许您访问stderr、返回状态码和stdout。</p>
<pre><code> $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);
</code></pre>
<p>与popen或exec等其他函数相比,proc_open的有趣之处在于它提供了特定于windows平台的选项,如:</p>
<pre><code>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
</code></pre>