开发者

php - feof error

开发者 https://www.devze.com 2022-12-30 20:32 出处:网络
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

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 开发者_StackOverflow中文版= 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");
    }
  }
}


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消