Setting default images

Discussion in 'PHP' started by aquasonic, Aug 9, 2007.

  1. #1
    Hi,

    I want to be able to put a simple if statement or something to stop broken images looking crap!

    I wan't a page to show an image of my choice if the img src isn't there.


    Basically - I'm adding the favicon.ico of others websites to my website... but if they havn't got a favicon I don't want it to show a placeholder - I want it to display a default icon.


    Something along the lines of

    if img src broken then display default.ico
    PHP:

    If anyone has any ideas, I'd be very appreciative.
     
    aquasonic, Aug 9, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    
    function remote_file_exists($url)
    {
        $headers = @get_headers($url);
        return preg_match('/^HTTP\/1\.\d\s+200/i', $headers[0]);
    }
    
    // Example:
    
    if (remote_file_exists('http://example.com/favicon.ico'))
    {
        // Exists
    }
    else
    {
        // Doesn't exist
    }
    
    PHP:

    Untested but should work. (PHP 5 is needed though)
     
    nico_swd, Aug 9, 2007 IP