1

I have following working code which read file contain and put into array but now i want to run command and place its output in array for example ls command

<?php
$path = "file.txt";
$file = fopen($path, 'r');
$data = fread($file, filesize($path));
 fclose($file);
    $lines =  explode(" ",$data);
                echo "<p><h1>$lines[0]</h1></p>";
?>

How do i read command output and place in array?

2 Answers 2

1

You can use shell_exec:

<pre><?php
$output = shell_exec('ls');
print_r($output);
Sign up to request clarification or add additional context in comments.

2 Comments

This does not convert the ls output into an array. I need the ls output into an array.
@RobbieSmith: and you are not able to use trim and explode?
1

My Preferred method would be to use popen. I would be trivial to put the results in an array

 $fp = popen ("ls -l", "r");
 $array = array();
 while ($rec = fgets($fp)){
     $array[] = trim($rec);
 }
 // do something cool with array

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.