Why is this 'not a valid stream resource?'

Hi Folks,

Check out the code snippet below. When run from within PHP Expert Editor I get the following message for each line it should write into the file url.dat.

Warning: fclose(): 2 is not a valid stream resource in F:\test.php on line 65

Anyone any clues?

Kind regards,

Dweep


$regels= fopen('url.dat', 'w');
 
    for($i=1;$i<(count($urls))+1;$i++) 
             {
		 if (!stristr($urls[$i],$sGoogle))
		         {
		         fputs($regels, $urls[$i])."\n";
		         }
		        fclose($regels);
             }

Open in new window

PeterdeBAsked:
Who is Participating?
 
dr_dedoConnect With a Mentor Commented:
you shold be having this error after your file is opened (provided that files is in place and readable)
see, in the end of every turn in  your loop you close the file, so, first you open the file, then at line 9 you close it, thin in the next turn, you close that file again, but it was not open that time, so it throws a warnning.
you should place that fclose after the loop ends or add some break; after it
0
 
hernst42Commented:
Try the following code. This might hapen if opening the file failed.
if (($regels= fopen('url.dat', 'w') === false) {
   die('Unable to open "url.dat" for writing');
}

Open in new window

0
 
PeterdeBAuthor Commented:
Thanks!
0
 
PeterdeBAuthor Commented:
Hernst42 thanks for responding, I tried your code but it didn't solve the problem.
Dr Dedo > I put the fclose outside the loop and that solved the problem.

Kind regards,

Dweep
0
All Courses

From novice to tech pro — start learning today.