开发者

Cannot using foreach loop with PHP exec() function?

I am trying to find out the disk volume name.And my code is:

$diskVolume = array('m','r');
foreach ($diskVolume as $volume) {
    echo $volume.' ';
    $cmd = 'fsutil fsinfo volumeinfo '.$开发者_开发知识库volume.':';
    exec( $cmd,$getVolumeName);            
    echo $getVolumeName[0].'<br /> ';
}

But my code seems only got the first item m's volume name and couldn't get the r.In other words ,the loop only get the first item's information..,

Thank you very much!!

information about fsutil: fsutil


exec second argument accepts an array by reference:

string exec ( string $command [, array &$output [, int &$return_var ]] )

If $output is already an array, it won't reinitialize the array, but append to it. Per example:

$output = array('foo');
exec('who', $output);
var_dump($output);

Yields:

array(2) {
  [0]=>
  string(3) "foo"
  [1]=>
  string(43) "netcoder tty7         2011-01-17 17:52 (:0)"
}

Reinitialize it yourself instead:

$diskVolume = array('m','r');
foreach ($diskVolume as $volume) {
    $getVolumeName = null; // reinitialize here
    echo $volume.' ';
    $cmd = 'fsutil fsinfo volumeinfo '.$volume.':';
    exec( $cmd,$getVolumeName);            
    echo $getVolumeName[0].'<br /> ';
}


<?php
$diskVolume = array('m','r');
foreach ($diskVolume as $volume) {
    echo $volume."\n";
    $cmd = 'echo '.$volume;
    exec($cmd,$getVolumeName);            
}
echo print_r($getVolumeName, true)."\n\n";

The above code executes both commands and outputs the results of BOTH commands as a SINGLE array at the end. You were only looking at the first entry in the array but exec with the output variable passed to it CONCATENATES onto the existing array if that variable you are passing has a value already.

http://php.net/manual/en/function.exec.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜