Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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 /> ';
}
share|improve this answer
    
it works.Thank you very much!! –  qinHaiXiang Jan 18 '11 at 2:59
<?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

share|improve this answer
    
Thanks for your help! –  qinHaiXiang Jan 18 '11 at 2:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.