Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP exec returns "array" instead of the result of my command

Tags:

linux

php

I'm trying to build a web terminal emulator on my local webserver (openwrt), every time I execute the command, the result always says "array", for instance when I execute uptime it doesn't give me the up time of my webserver but instead it gives me array. This is my script:

<?php
..............................
if($_POST['command'])
{
$command = $_POST['command'];
exec("$command 2>&1 &", $output);
echo $command;
}
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "Command:<br><input type=\"text\" autofocus name=\"command\" size=\"15\" value=\"\"/><br>";
echo '<br>Result :<br><pre>
        <div id="show" style="font-size: 11px;  word-wrap: break-word; width:550px;height:200px;border:0px solid #000;text-align:left; overflow-y: scroll;">
        '.$output.'</div>';
echo '<input type="submit" name="kill" value="Kill Command" />';
echo "</form></div>";
.........................
?>

I want the $output to return the result of my command. Right now, the $output always returns array no matter what type of command I execute, how should I fix this?

like image 269
Don Avatar asked Dec 25 '22 23:12

Don


1 Answers

It is supposed to return an array as it is written in the documentation.

If you want to print the array line by line use a code like this:

<?php
exec("$command 2>&1 &", $output);
foreach ($output as $line) {
    echo "$line\n";
}
like image 172
Attila Fulop Avatar answered Feb 19 '23 19:02

Attila Fulop