denn0n
denn0n

Reputation: 165

Php shell command output on to a variable

Hi I'm trying to pass a Shell Command to a variable in php and I'm doing

$var=system('ls');

and it is executed but the output it is echo as default not in the variable i use to

$var=exe('ls');

or

$var=exec('ls');

but print_r just print the last file on the list, why it is happen ? and how i get to do so ?

Upvotes: 3

Views: 2628

Answers (1)

Cyrus
Cyrus

Reputation: 88899

Use shell_exec:

$var=shell_exec('ls');

or exec:

exec('ls', $output, $return_var);

# print array
foreach($output as $content){
  echo $content . "\n";
}
print "return_var:" . $return_var . "\n";

Upvotes: 4

Related Questions