Run subcommand and return output to variable in PHP

If you need to run a subcommand and pull the output you can use the proc_open like below.

Kevin

 

$descriptorspec = array(

0 => array(“pipe”, “r”),  // stdin is a pipe that the child will read from

1 => array(“pipe”, “w”),  // stdout is a pipe that the child will write to

2 => array(“file”, “/tmp/yourprocoutput.txt”, “a”) // stderr is a file to write to

);

$process = proc_open(“/bin/ls -l /tmp”, $descriptorspec, $pipes);

while (!feof($pipes[1])) {

$result .= fread($pipes[1], 8192);

print “.”;

}

print “RESULT: [$result] \n”;

?> 

This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published.