note 23228 added to function.popen
| From: | php-general at lists dot php dot net | Date: | Fri, 12 Jul 2002 06:33:25 +0000 |
| Subject: | note 23228 added to function.popen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-32947@lists.php.net to get a copy of this message | ||
Here is a workaround for not having bidirectional pipes in php.
If you have bidirectional pipe support, don't bother with this.
The trick here is to send the input on the command line to the target application. In particular I
wanted to use openssl without using temp files or named pipes. This solution should also be
thread/process safe.
This does work on Linux (RedHat 7).
function filterThroughCmd($input, $commandLine) {
$pipe = popen("echo \"$input\"|$commandLine" , 'r');
if (!$pipe) {
print "pipe failed.";
return "";
}
$output = '';
while(!feof($pipe)) {
$output .= fread($pipe, 1024);
}
pclose($pipe);
return $output;
}
# example:
print filterThroughCmd("hello", "cat");
# Piping to cat has the effect of echoing your input.
--
http://www.php.net/manual/en/function.popen.php
http://master.php.net/manage/user-notes.php?action=edit+23228
http://master.php.net/manage/user-notes.php?action=delete+23228
http://master.php.net/manage/user-notes.php?action=reject+23228