note 90342 added to function.proc-open
| From: | mattalexxpub at gmail dot com | Date: | Fri, 17 Apr 2009 01:37:33 +0000 |
| Subject: | note 90342 added to function.proc-open | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-153417@lists.php.net to get a copy of this message | ||
@missilesilo at gmail dot com
Your class is cool but I didn't get it working until I fixed one little typo:
This:
<?php
$descriptorspec = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'r')
);
?>
Should be this:
<?php
$descriptorspec = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w')
);
?>
Or the method hangs, waiting for input into stderr. So here's the corrected method:
<?php
class Process
{
public static function open($command)
{
$retval = '';
$error = '';
$descriptorspec = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w')
);
$resource = proc_open($command, $descriptorspec, $pipes, null, $_ENV);
if (is_resource($resource))
{
$stdin = $pipes[0];
$stdout = $pipes[1];
$stderr = $pipes[2];
while (! feof($stdout))
{
$retval .= fgets($stdout);
}
while (! feof($stderr))
{
$error .= fgets($stderr);
}
fclose($stdin);
fclose($stdout);
fclose($stderr);
$exit_code = proc_close($resource);
}
if (! empty($error))
throw new Exception($error);
else
return $retval;
}
}
try
{
$output = Process::open('cat example.txt');
// do something with the output
}
catch (Exception $e)
{
echo $e->getMessage() . "\n";
// there was a problem executing the command
}
?>
----
Server IP: 208.69.120.35
Probable Submitter: 67.42.63.210
----
Manual Page -- http://www.php.net/manual/en/function.proc-open.php
Edit -- https://master.php.net/note/edit/90342
Del: integrated -- https://master.php.net/note/delete/90342/integrated
Del: useless -- https://master.php.net/note/delete/90342/useless
Del: bad code -- https://master.php.net/note/delete/90342/bad+code
Del: spam -- https://master.php.net/note/delete/90342/spam
Del: non-english -- https://master.php.net/note/delete/90342/non-english
Del: in docs -- https://master.php.net/note/delete/90342/in+docs
Del: other reasons-- https://master.php.net/note/delete/90342
Reject -- https://master.php.net/note/reject/90342
Search -- https://master.php.net/manage/user-notes.php