note 23228 modified in function.popen by danbrown
| From: | danbrown@php.net | Date: | Mon, 02 Mar 2009 13:41:55 +0000 |
| Subject: | note 23228 modified in function.popen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-151339@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).
<?php
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.
?>
--was--
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://php.net/manual/en/function.popen.php