FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » redirect stdout and stderr to PHP variables?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: redirect stdout and stderr to PHP variables? [solved] [message #176890 is a reply to message #176799] Wed, 01 February 2012 12:22 Go to previous message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma:
Senior Member
On 01/25/2012 12:47 PM, crankypuss wrote:
> I'm sure there's a way to do this, probably some simple syntax I've not
> run into and am too stupid to find in the manual or through google-fu or
> otherwise.
>
> I'd like to issue an arbitrary shell command in a subroutine and have it
> return an array that contains one element representing stdout and
> another element representing stderr.
>
> For example, using tar to deal with a bazillion files, error messages
> might not be seen when specifying verbose output; I'd like to collect
> them and display after stdout.
>
> I've not found any syntax, yet, for redirecting to a PHP variable rather
> than some file.
>
> tia.

Thanks for your help, folks. This seems to do the trick:


// executes $cmd as a separate process and returns the following:
// $result = null if unable to start process, else:
// $result["exit_code"] = exit code returned by $cmd
// $result["stdout"] = stdout written by $cmd
// $result["stderr"] = stderr written by $cmd
function sysCmd($cmd)
{
$result = array();
$descr = array();
$descr[0] = array("pipe", "r"); // stdin
$descr[1] = array("pipe", "w"); // stdout
$descr[2] = array("pipe", "w"); // stderr
$proc = proc_open($cmd, $descr, $pipes);
if (is_resource($proc))
{
fclose($pipes[0]); // no stdin data
$result["stdout"] = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$result["stderr"] = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$result["exit_code"] = proc_close($proc);
}
return $result;
}
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: enter search term via code?
Next Topic: FounderDating - Helping entrepreneurs find their co-founder & start companies!
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 21:05:35 GMT 2024

Total time taken to generate the page: 0.04461 seconds