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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
dr_dedoCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.