1

In php I wish to display the result of a command output just as it would be shown on the console. To simplify this example we'll use ifconfig. The problem is php runs all lines together instead of dispaying it properly. I have tried to use shell_exec(), passthru(), and system() but they all display the same.

Using these two question as a reference I though passthru() would be the answer.

PHP - exec() vs system() vs passthru()

http://www.sitepoint.com/forums/showthread.php?525977-multiline-result-in-exec

Here is the php code:

 <?php
    $output = passthru("ifconfig");
    echo $output;
 ?>

This is what is displayed in the browser: enter image description here

Community
  • 1
  • 1
Cliff Ennis
  • 33
  • 1
  • 6
  • 2
    Try adding `header('Content-Type: text/plain');` Since you probably haven't told it otherwise, your browser will assume this is HTML. In HTML, `\r` or `\n` aren't rendered new lines. – Jonnix Oct 28 '16 at 21:13
  • 2
    This is your browser doing what browsers do.. eg; ignore white space. try `echo '
    '. $output .'
    ';`
    – Duane Lortie Oct 28 '16 at 21:14
  • Slight amendment, the browser won't assume it's text/html, it's apparently PHP's default. – Jonnix Oct 28 '16 at 21:22

1 Answers1

0

You can use the Content-Type header to get the text from passthru as it comes.

<?php
    header('Content-Type: text/plain');
    $output = passthru("ifconfig");
    echo $output;
 ?>