Error: not valid stream resource (in a class)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
murfy
Forum Newbie
Posts: 8
Joined: Sat Dec 07, 2013 2:46 pm

Error: not valid stream resource (in a class)

Post by murfy »

HI, I need a little help
http://pastebin.com/ardzHaUh
See lines 392 - 398

Code: Select all

<?php 
include("scan_ips_class.php");
$scanIPs = new Scan_ips;
?>
When I run this script I got error:
Warning: fseek(): 5 is not a valid stream resource in P:\server\scan_ips\scan_ips_class.php on line 402

Whilst I understand that the file hanlder is not valid, I cannot find out why it is not valid.
There is this line:
if ( !$this->FRWBHandler )
$this->FRWBHandler = @fopen($this->file,"r+b");
which should set the file hanlder. In fact, I see that the file handle is set, because I print it by echo. If I will add this line:

Code: Select all

$this->FRWBHandler = @fopen($this->file,"r+b");
So it will not generate any error, but it will generate new resource id which is not what I wanted to do.

Just to explain what I do in the class:
I add IPs into file, but before I add it I want to check if it already exist or not, so I run function to check it. So if I call the check, the file will be opened so I won't need to close it and then reopen it again. This is why I added the shared file handler called $this->FRWBHandler

So my question is what is wrong that is does not work as I expect. Either it will not accept the resource (resource 5) or it will generate new resources If I will ignore that the resource is already opened.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Error: not valid stream resource (in a class)

Post by Eric! »

Odds are good it can't read the file for whatever reason and if you remove the @ you'll see the error_warning in your log.

Code: Select all

$this->FRWBHandler = fopen($this->file,"r+b");
Also what is (!$this->FRWBHandler) ? If for some reason it is not false you might not be calling fopen.
murfy
Forum Newbie
Posts: 8
Joined: Sat Dec 07, 2013 2:46 pm

Re: Error: not valid stream resource (in a class)

Post by murfy »

Solved. I had to move fclose.
Post Reply