-1

I ended up “resolving” someone else’s code issue since the site was down and we couldn’t get a hold of them, but I don’t use PHP and I think this is working, but not for the right reasons:

They have something like:

$file = ../somedir/somefile.txt;
$fh = fopen($file, 'r');
while (!feof($fh)) {
$line = fget(...

But the page wasn’t loading.

Based on the error in the log, I ended up changing the feof line to:

while (feof(False, $fh)) {

Which actually appears to have resolved the issue, but after looking at documentation for feof(), I don’'t think what I did was right at all.

What is that impact of adding False as the first argument to PHP's feof() function?

Update: The log error was feof() expects parameter 1 to be resource, boolean given in I took Boolean to mean True False, which is why I set the False as first arg.

GL2014
  • 6,016
  • 4
  • 15
  • 22
  • Can you show what the error in the log was? That will help in determining what the best solution is. – server_kitten May 30 '14 at 02:56
  • I can't see how the code could still be functioning (specifically the body of the original loop including `fgets()`. You have changed the negative condition in the loop to a positive one, but `feof()` accepts only one parameter. PHP would be throwing tons of E_WARNING as `Warning: feof() expects exactly 1 parameter, 2 given` – Michael Berkowski May 30 '14 at 02:56
  • agreed, feof only takes one parameter, a resource. if anything, this should have caused more problems. – server_kitten May 30 '14 at 02:58
  • The error in the logs was "feof() expects parameter 1 to be resource, boolean given in " – GL2014 May 30 '14 at 02:58
  • Ahhhhhh! The original error _feof() expects parameter 1 to be resource, boolean given_ indicates that the `fopen()` call before it failed and returned `false` instead of a file handle resource. You need to fix the `fopen()` to make sure it can read the file. – Michael Berkowski May 30 '14 at 02:59
  • After `$fh = fopen(....)` check `if ($fh) { // loop over the file } else { // Oops, an error opening the file }` – Michael Berkowski May 30 '14 at 02:59
  • So am I just making it circumvent the file? This code must be really messed up then to not need this block, because other blocks supposedly depend on it. – GL2014 May 30 '14 at 02:59
  • The fopen() points to a variable which contains a relative path i.e. ../somedir/file.txt is that problematic? – GL2014 May 30 '14 at 03:00
  • If fopen() is unable to read the file, check the permissions on the file and that the path to the file is correct. – server_kitten May 30 '14 at 03:01
  • @GreggLeventhal The relative path isn't problematic, but the file it leads to might be. It might not be readable by the web server user, or it might just not even exist. – Michael Berkowski May 30 '14 at 03:01
  • No, it exists and is readable by apache, I've checked that. – GL2014 May 30 '14 at 03:02
  • 1
    _“I took Boolean to mean True False, which is why I set the False as first arg.”_ – If you can not even interpret the error message the right way, then you probably shouldn’t be messing with this at all … – CBroe May 30 '14 at 03:04
  • @CBroe Probably not, but a site was down, no one else was available, I was able to understand what the block that was throwing errors was trying to do. I realized fairly quickly that I misinterpreted the error, and that is why this question exists. I want to know what the result of that misinterpretation actually was. – GL2014 May 30 '14 at 12:51

1 Answers1

1

What is that impact of adding False as the first argument to PHP's feof() function?

Well, here is what feof documentation says:

Tests for end-of-file on a file pointer.

And since there is only one parameter allowed in the function as indicated here:

bool feof ( resource $handle )

The False you are setting basically is telling the script:

while ([the file handle doesn’t equals false do this]) {

So you are basically short-circuiting the logic in some way. But without seeing the full code or error messages it’s 100% unclear what impact doing something like this would have on overall application behavior.

UPDATE: Since you now say the error log says:

feof() expects parameter 1 to be resource, boolean given in

That basically means the problem is the $file that you are attempting to open and then create a file handle with $fh does’t exist or can’t be read:

$file = ../somedir/somefile.txt;
$fh = fopen($file, 'r');

The best solution for you now is to wrap the condition in another conditional like this:

$file = ../somedir/somefile.txt;
if (file_exists($filename)) {
  $fh = fopen($file, 'r');
  while (!feof($fh)) {
  $line = fget(...
}

But now looking at your code is that line $file = ../somedir/somefile.txt; correct? Shouldn’t it be:

$file = '../somedir/somefile.txt';

So your code would look like this:

$file = '../somedir/somefile.txt';
if (file_exists($filename)) {
  $fh = fopen($file, 'r');
  while (!feof($fh)) {
  $line = fget(...
}

But past any of that, I would check if the actual file ../somedir/somefile.txt exists and is readable by your script. It could be that the location changed or the permissions or user connected to that file has been changed. That is most likely the underlying cause of the feof() expects parameter 1 to be resource, boolean given in error.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • This error stopped once I added my misguided False to feof. – GL2014 May 30 '14 at 03:03
  • @GreggLeventhal Read my full answer again. The issue is the `False` short circuited the logic. But the underlying issue is the `../somedir/somefile.txt` file itself cannot be read or doesn’t exist. My `if (file_exists($filename)) {` is a more elegant short circuit from a logic standpoint but it doesn’t change the underlying issue. – Giacomo1968 May 30 '14 at 03:06