...

View Full Version : How to stop an error showing - file()



Richard
01-03-2008, 03:49 AM
One of the scripts I'm using accesses a feed and returns the gathered information on my webpage. Recently however I've been checking the live webpage and the following error often shows up:


Warning: file() [function.file]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/202/i/domain.com/user/htdocs/index.php on line 44

Warning: file(http://feeds.domain.com/cat/feeds/rss/cat/cat/feed.xml) [function.file]: failed to open stream: No such file or directory in /home/202/i/domain.com/user/htdocs/index.php on line 44

Warning: Invalid argument supplied for foreach() in /home/202/i/domain.com/user/htdocs/index.php on line 46

Is it possible to print an error message saying 'This information is currently unavailable due to a technical error' rather than having all of this printed on the webpage? I hope that makes sense.

Fumigator
01-03-2008, 04:54 AM
Proper coding will fix you right up. Use is_file() to check to see if a file exists before you try to use the file. Use is_dir() to see if a directory exists before you try to point to that directory. Use is_array() to check to make sure a variable is an array before you try to use it in a foreach().

Fou-Lu
01-03-2008, 06:06 AM
Proper coding will fix you right up. Use is_file() to check to see if a file exists before you try to use the file. Use is_dir() to see if a directory exists before you try to point to that directory. Use is_array() to check to make sure a variable is an array before you try to use it in a foreach().
Agreed, you should always make proper error checking to completely remove errors.
But to actually answer your question, its with an @:
@file(...)

Fumigator
01-03-2008, 06:10 AM
The @ just masks bad code. :thumbsup:

kbluhm
01-03-2008, 06:26 AM
Use CURL to fetch the file, then check the response code with curl_getinfo(). If it's 200, you're good to go (and maybe 201, 202, etc). Also check the return length. If it's empty, not formatted as you'd expect, or unable to be parsed, then something bad happened. Either way, you're able to show your own error message.


$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'http://www.example.com/path/file.ext' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, FALSE );
$response = curl_exec( $ch );
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );

if ( !in_array( $http_code, range( 200, 206 ) ) OR empty( $response ) )
{
exit( 'My own error message...' );
}

// do what you'd like with $response here

Fou-Lu
01-03-2008, 06:28 AM
The @ just masks bad code. :thumbsup:
Agreed, says the lazy programmer lol



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum