Link to home
Start Free TrialLog in
Avatar of projects
projects

asked on

PHP Warning feof() fread() filling error logs and drive

Something about this small piece of code is filling my drive with ssl error logs at an incredible rate.
The web server has about 980GB of space but it fills to the brink in only hours when this starts happening.

It isn't someone hacking, it's a remote logging device trying to connect to pick up an update but it just keeps trying. I'm not a programmer but I need to stop this problem if possible without removing the php file so things keep running.

I am pretty sure it's because the file the remote is looking for doesn't exist at the moment but this error filling the drive should not happen of course.

[Wed Jul 16 04:18:38 2014] [client 216.19.18.217] PHP Warning:  feof() expects parameter 1 to be resource, boolean given in /html/myapp.php on line 41

[Wed Jul 16 04:18:38 2014] [client 216.19.18.217] PHP Warning:  fread() expects parameter 1 to be resource, boolean given in /html/myapp.php on line 42

Section of code is;

if($_SERVER['REQUEST_METHOD'] === 'GET'){
    $file = fopen("myapp/file1","r");
LINE 41    while (!feof($file)){
LINE 42        echo fread($file,1024);

Long shot but thought I'd give it a try :)
Avatar of kaufmed
kaufmed
Flag of United States of America image

Are all 4 of those lines consecutive, or is there stuff in between the fopen call and the while loop?
Avatar of projects
projects

ASKER

Yes, consecutive so no error catching of any kind I guess.
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is replicable with the following code

<?php
$file = fopen("nosuchfile.nul", "r");
while(!feof($file)) {
	echo fread($file, 1024);
}

Open in new window


It does exactly what you are seeing - i.e. report hundreds of log messages.

This is because the file that is being opened does not exist - because you are not checking for a failure on the file open the code is entering an infinite loop trying to read from a non-existent file.

The feof() function does not interpret a FALSE value returned from the failed fopen as an end of file condition so the while does not terminate.

Solution: I think Dave Baldwin's post is the correct solution to the problem.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So now my code looks as follows;

    if($_SERVER['REQUEST_METHOD'] === 'GET'){
    $file = fopen("myapp/file","r");
    if (!$file) trigger_error('UNABLE TO OPEN myapp/file FOR READ', E_USER_ERROR);
    //if($file) {
    while (!feof($file)){
    echo fread($file,1024);
      //}
     }
    fclose($file);
       }
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
...reformatted with comments.
+1 for that.  Always a great idea.
Always a great idea.
Except that comments should indicate why you are doing something, not what you are doing. If someone can't tell what you are doing by looking at your code, then you've written it badly.
I disagree @kaufmed.  Complexity by itself can make it difficult to tell what is going on.  I'm working on code where I have to check up to 20 different conditions to create an output.  And they're not mutually exclusive.  The conditions are used in different combinations.  I've got charts and spreadsheets just trying to organize it well enough to write a working version.
I've got charts and spreadsheets just trying to organize it well enough to write a working version.
Then I would say that your solution isn't architected well. But that's me playing Monday-morning quarterback without knowing a thing about your project. I'm sure there will be some cases where you simply don't have a choice, but in my opinion most cases should have code that reads like what it does. There are countless tools available these days to make writing code a simple task. This isn't the 80's where you were limited to 8 character variable names, and object-oriented programming can do a lot to simplify (and in the worst case complicate) the readability of code. And yes, in those (IMO rare) situations where you simply cannot architect a simple approach to a complex problem, commenting the what would make more sense--but that's not to imply you leave out the why.

As it stands, julianH's comments in the code add no value in a production environment; in a learning environment--like this forum--the comments could hold value (depending on the skill of the reader). Even for someone who doesn't work in PHP, it should be somewhat obvious what fopen is doing--why do I need a comment to tell me that? But again, I only assert this based on whether or not this code is migrated into a production system. In this thread, I have no problem with it.
I think at this level, when the Author does not really understand PHP, the what may be as important as the why, and I often write comments like that  in code samples here at E-E.  And I agree that in a deployable application saying that fopen() opens a file is too basic to warrant a comment; the appropriate comment would probably be a Docblock at the top of the method definition.
A shortening attention span doesn't help either...  I write comments to tell myself what I thought I was doing because nobody else ever sees my code.  Except for demos here.
@DaveBaldwin

I've learned that the hard way myself  = )
@kaufmed
There are many different reasons for commeting code - some of which you have covered. In this forum you will no doubt have seen many examples where code has been commented (in many cases by some of the posters in this thread) for the purpose of helping the asker understand the code.

Well written code in many cases is self commenting - although I am not sure your comment about OO (over 80's coding style) is necessarily valid - sometimes OO code can be pretty confusing when you have to trace functionality back up the hierarchy across multiple files.

In this particular post documenting the why was not possible because I have aboslutely no idea what the author's code is for. My point was that when you post code to EE it helps to format it correctly and provide comments so that the rest of us can understand what is being asked - I then gave an example.

I don't think my answer should have been the accepted solution as I was simply re-phrasing what others have written - my post was aimed primarily at giving pointers - and not as a guide on how to comment code.
@julianH

Please don't think I was attacking your code personally. I was simply trying to make the point of what a comment should contain. Commenting the what is probably 75% of the comments we experts post here, because we are attempting to help someone who might not be as experienced as we are. I totally get that. My attempt to help "projects" was to stress that comments in code need to have meaning--albeit I can see how my initial comment didn't clearly emphasize this.

Personally, I hate typing.  As such, I always try to self-document as much as I can  = )
You are right, I picked the wrong answers in error.
I do too many things at once :(
Hello,
I think I have exactly the same problem the disc usage runs full in minutes and the error log is

(PHP Warning:  feof() expects parameter 1 to be resource, boolean given in /home/qgzs8bjq/public_html/wp-content/wp-mbrxsmrs.php)

million times so the file gets huge.

which code do I have to place and were do I have to place it please?

Thanks folks