OK I actually figured out how to encrypt and decrypt using PHP and GPG. My original code (for some reason) didn't add the $msg to the popen function. Therefore I got an encryted file with nothing in it. It only took me about 15 hours to figure that out!!!!
Anyway using the shell_exec () command I've manged to successfully create a gpg key (mean't to be inputted into a database) then the decrypt part takes that variable and puts it into a temporary file which can then be read into gpg (I could find any way of taking a variable and decrypting it on the fly).
I really would appreciate it if anyone out there can tell me why my original popen and fputs commands didn't actually get entered into the command.
Anyway I guess theres going to be someone out there that'll want to see how they can encrypt and decrypt a gpg message within PHP so I've included the code below (maybe it'll save you the 15 hours I spent on it!!):
<?php
#!/usr/bin/php
/* set up some strings */
$gpgkeydir = "/home/userdir/keys";
$gpg = '/usr/bin/gpg';
$uid = "me@mydomain.com";
$msg = "This is a test message";
$passphrase = "mypassphrase";
$file = "/home/userdir/keys/encode.gpg";
putenv("GNUPGHOME=$gpg");
/* OK now let's encrypt the $msg */
$encrypted = shell_exec("echo $msg | $gpg --batch --no-secmem-warning --no-tty --yes -ea --always-trust --homedir $gpgkeydir -r $uid");
print "<form><textarea NAME=txt ROWS=20 COLS=75 WRAP=VIRTUAL>";
print $encrypted;
print "</textarea><br>";
/* Now the decrypt part */
// this bit makes a file
touch($file);
$df = fopen($file, "w") or die ("Couldn't open $file");
fwrite($df, $encrypted);
fclose($df);
// now decode that file
putenv("GNUPGHOME=$gpg");
global $gnupghome;
global $path_to_gpg;
$decrypted = shell_exec("echo $passphrase| $gpg --passphrase-fd 0 --batch --no-secmem-warning --no-tty --yes --homedir $gpgkeydir -d $file");
print "your decoded message is:<br>";
//
echo " <textarea cols=80 rows=20 name=plaintext>$decrypted</textarea>";
// delete file
unlink($file);
?>
Apparently this method is less secure than the popen method. Again if anyone could tell me why (plus how I could get the other method to work) I'd appreciate it.