andrepcg Posted February 8, 2009 Share Posted February 8, 2009 i have a question. is there any way to do this: if file_get_contents fails echo "Couldn't reach the address" how can i translate that into PHP language? Quote Link to comment https://forums.phpfreaks.com/topic/144368-file_get_contents-question-help-quickly/ Share on other sites More sharing options...
wildteen88 Posted February 8, 2009 Share Posted February 8, 2009 Do: if($contents = file_get_contents('path.to.file')) { // do something with $contents } else { // display error message } Quote Link to comment https://forums.phpfreaks.com/topic/144368-file_get_contents-question-help-quickly/#findComment-757557 Share on other sites More sharing options...
andrepcg Posted February 9, 2009 Author Share Posted February 9, 2009 what variable is $contents? Quote Link to comment https://forums.phpfreaks.com/topic/144368-file_get_contents-question-help-quickly/#findComment-758163 Share on other sites More sharing options...
unkwntech Posted February 9, 2009 Share Posted February 9, 2009 $contents would be a new variable that would be asssigned to the return value of file_get_contents. I think this will also work: $var = file_get_contents(...) or die('Could not connect'); Quote Link to comment https://forums.phpfreaks.com/topic/144368-file_get_contents-question-help-quickly/#findComment-758167 Share on other sites More sharing options...
Maq Posted February 9, 2009 Share Posted February 9, 2009 $contents would be a new variable that would be asssigned to the return value of file_get_contents. I think this will also work: $var = file_get_contents(...) or die('Could not connect'); Although it would work, it would also kill the script if file_get_contents() doesn't return anything. Which is probably not what the OP wants. Quote Link to comment https://forums.phpfreaks.com/topic/144368-file_get_contents-question-help-quickly/#findComment-758225 Share on other sites More sharing options...
andrepcg Posted February 9, 2009 Author Share Posted February 9, 2009 this is my script: $url = "blablablablalba"; $ctx = stream_context_create(array( 'http' => array( 'timeout' => 10 ) ) ); $buffer = file_get_contents($url, 0, $ctx); please tell me exactly how to make the return if die function. Quote Link to comment https://forums.phpfreaks.com/topic/144368-file_get_contents-question-help-quickly/#findComment-758255 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.