0

I have following php script in which I am trying to execute a command on Windows and print it's output. I have tried system(), shell_exec(), passthru() and exec().

$cmd = $_POST['cmd'];
$result = array();
exec($cmd, $result);
foreach ($result as $line ){
 echo $line."<br>";
}

exec($command, $array) is the closest to my expectation of printing everything when a command is executed. I wish to print everything even though it's an error. But it only prints when command is executed successfully.

How to achieve it ?

Rahul
  • 2,658
  • 12
  • 28

1 Answers1

2

$result only contains the standard output of the command, but error messages are usually written to standard error. You need to redirect stderr to stdout when running the command.

$cmd = "$cmd 2>&1"
exec($cmd, $result);

In the shell, what does " 2>&1 " mean?

Barmar
  • 741,623
  • 53
  • 500
  • 612