tags:

views:

39

answers:

3

The file that I'm trying to read is a pgp-encrypted file. This is part of the process to decrypt it and I'm actually attempting to read the contents into a string so that I can then decrypt it. I'm not sure if that's the core problem here or not, but I'm getting an error:

Warning: feof(): supplied argument is not a valid stream resource

Here's the file code:

if($handle = opendir($dir)) {
  while( false !== ($file = readdir($handle))) {
    if($file != "." && $file != "..") {
      $fhandle = fopen($file, "r");
      $encrypted = '';
      $filename = explode('.',$file);
      while(!feof($fhandle)) {
        $encrypted .= fread($fhandle, filesize($file));
      }
      fclose($fhandle);
      $decrypted = $filename[0].'.txt';
      shell_exec("echo $passphrase | $gpg --passphrase-fd 0 -o $decrypted -d $encrypted");
    }
  }
}
A: 

You should check the fopen call to make sure you're file has actually been opened. Check it's return value.

As for fixing it you're working directory is likely different than $dir. You probably need

fopen("$dir/$file","r"); 

unless you change to the directory first. edit clarified that the code sample was a possible solution to the problem, not code to check the return value.

Cfreak
A: 

You need to check your return values. The error indicates that $fhandle doesn't contain a valid file handle - it probably contains false, which fopen returns on failure.

See http://ca.php.net/manual/en/function.fopen.php

meagar
+1  A: 

Learn to debug your code.

supplied argument is not a valid stream resource means passed variable contains unexpected value. So, we can make a logical conclusion, that a function returning this variable had fail. So. we have to check fopen($file, "r"); what makes it fail? may be we can check if a file exists? And so on.

This is called debugging and you cannot program without it.

Though it seems very strange. Because fopen should throw an error as well.

Col. Shrapnel
Really? You have to be *that* nasty? I would assume that fopen *would* throw an error, thus my question. But, really, if you can't manage civility, just don't respond. Life is hard enough without having to deal with people that can't refrain from being a dick.
TwixxyKit
So, don't deal. You can ignore my answer as well ;)
Col. Shrapnel
But let's make peace. Honestly, @TwixxyKit your case is very strange and I anxious to find it out. Pardon my tone but it is because of your `"I'm not sure if that's the core problem" [so find it out for me]`. Sounds a bit ignorant. Anyway. What way you have got your feof error message? it was printed on the screen? is is display once? Is it the actual code you run? is there any @ symbols?
Col. Shrapnel