validate file_get_contents

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
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

validate file_get_contents

Post by GeXus »

Does anyone know how to validate file_get_contents?

Im using the following

Code: Select all

if(file_get_contents("url") !== FALSE){// do stuff}
But this does not work.. i recieve the failed to open stream error...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: validate file_get_contents

Post by volka »

GeXus wrote:But this does not work.. i recieve the failed to open stream error...
And what do you expect?
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

I would expect it to do nothing.. or if i do

Code: Select all

if(file_get_contents("url") !== FALSE){echo "good";}else{echo "bad";}
Then I would expect it to display "bad", which it does not.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

It works fine for me, also you may want to use @ for error supression, if you wanted to handle your own errors

Code: Select all

if(!$file = @file_get_contents("url")){ 
   echo $file;
}else{
   echo "bad";
}
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Jcart, Yes that does work.. where FALSE does not.. thanks!

Could you explain how the @ works? Thank You.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

GeXus wrote:Jcart, Yes that does work.. where FALSE does not.. thanks!

Could you explain how the @ works? Thank You.
Like I said already, it supresses the error so you can handle the error however you please. In your case, if the file didn't exist you'd get a warning saying file does not exist, therefore we use the @ to hide this error.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Jcart wrote:
GeXus wrote:Jcart, Yes that does work.. where FALSE does not.. thanks!

Could you explain how the @ works? Thank You.
Like I said already, it supresses the error so you can handle the error however you please. In your case, if the file didn't exist you'd get a warning saying file does not exist, therefore we use the @ to hide this error.
Gotcha! Thank you
Post Reply