Kin
Kin

Reputation: 4596

How to get an output from exec?

I tried different variations of exec:

exec('which ffmpeg', $output, $e);
exec("which ffmpeg", $output);
exec('which ffmpeg 2>&1', $output, $e);
exec('echo "which ffmpeg" 2>&1', $output, $e);
$output = exec('which ffmpeg');

But no luck.

In console:

[root@gs01]# which ffmpeg
/usr/local/bin/ffmpeg

Upvotes: 2

Views: 502

Answers (3)

brainstronaut
brainstronaut

Reputation: 1

echo shell_exec('which ffmpeg');

shell_exec returns the output as string.

Upvotes: 0

Loic
Loic

Reputation: 90

system() might do what you want.

ob_start();
system('ls 2>&1');
$ob_contents = ob_get_contents();
ob_end_clean();


echo $ob_contents;

Upvotes: 0

marian0
marian0

Reputation: 3337

Pass array as second argument to exec() method:

$output = array();
exec('which ffmpeg', $output);
var_dump($output);

If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

For more information check manual.


php > $a = array(); exec('which ffmpeg', $a); var_dump($a);

Returns:

array(1) {
  [0] =>
  string(15) "/usr/bin/ffmpeg"
}

Upvotes: 2

Related Questions