how to open pipe for writing and then read its output?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vasilis
Forum Commoner
Posts: 40
Joined: Tue Apr 22, 2003 7:37 am

how to open pipe for writing and then read its output?

Post by vasilis »

Here is the main part of my gnupg encryption function for encrypting text contained in the '$plain_text' variable and outputting it to the '$encrypted_text' variable:

Code: Select all

$command =  "$gpg -a --recipient " . ""$public_key_id"" . " --encrypt";
$pipe=popen($command, "w");
$no_ofbytes_written=fwrite($pipe, escapeshellarg($plain_text));
$encrypted_text = '';
while($s = fgets($pipe, 1024)) {  
 $encrypted_text .= $s;
 }
 pclose($pipe);
return $encrypted_text;
I have checked the code and I know that a number of bytes is written to the pipe (the '$plain_text' variable content is written in the pipe, since I get non-zero return from the fwrite() function - $no_ofbytes_written<>0). SO, I guess the encryption works ok. The problem is that I cannot read from the pipe with the fgets() function, since it has been opened with the "w" argument. If I try to open it for reading (with "r" argument), the 'fgets()' function writes zero bytes to it . Therefore, I need to find a way to re-open the same pipe for reading (and of course keep its contents) (i dont want to use physical files since they are insecure for pgp encryption of sensitive data).
Can anybody help me to that ??? In a few words, how can you open a pipe for writing, write to it, and then read from it ??
evilMind
Forum Contributor
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

Post by evilMind »

do you have safe mode enabled?
vasilis
Forum Commoner
Posts: 40
Joined: Tue Apr 22, 2003 7:37 am

Post by vasilis »

Does safe mode have to do with the above? I have checked that code and it works ok, anyway in my local computer. My question is, again, how do I open a pipe for writing, feed it with input, and read its output ??
evilMind
Forum Contributor
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

Post by evilMind »

Yes. Safe mode *does* have to do with the above. Safe mode can cause your pipes to behave uncommonly. eg: grep -Hi pattern * | cut -b 10- woud turn into grep -Hi pattern "* | cut -b -10-" having astonishingly different results then the aforementioned.
vasilis
Forum Commoner
Posts: 40
Joined: Tue Apr 22, 2003 7:37 am

Post by vasilis »

I do not have safe mode enabled, neither in my local testing computer, nor in the server where my account has been tested.
The thing is that regardless of safe mode, my question has a theoretical dimension... i.e. how one can open a pipe for writing, send to it some input, and then read the output from it to a variable... (since the pipe was opened for writing with the "w" parameter, then in order to read from it, shouldn't it be opened for reading (with "r" parameter)???. But, before openint it again for reading, shouldn't it be closed, an action that would mean losing its data???
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

pipes are uniderectional. use [php_man]proc_open[/php_man] instead.
vasilis
Forum Commoner
Posts: 40
Joined: Tue Apr 22, 2003 7:37 am

Post by vasilis »

thanks, that sounds like its gonna work... I ll try it out....
Post Reply