Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    England
    Posts
    172
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to stop an error showing - file()

    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.
    \o/

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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().

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by Fumigator View Post
    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(...)
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The @ just masks bad code.

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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.
    PHP Code:
    $ch curl_init();
    curl_setopt$chCURLOPT_URL'http://www.example.com/path/file.ext' );
    curl_setopt$chCURLOPT_RETURNTRANSFERFALSE );
    $response  curl_exec$ch );
    $http_code curl_getinfo$chCURLINFO_HTTP_CODE );
    curl_close$ch );

    if ( !
    in_array$http_coderange200206 ) ) OR empty( $response ) )
    {
        exit( 
    'My own error message...' );
    }

    // do what you'd like with $response here 
    Last edited by kbluhm; 01-03-2008 at 05:38 AM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by Fumigator View Post
    The @ just masks bad code.
    Agreed, says the lazy programmer lol
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •