Snippets

Test whether URL exists

Test if an url exists or not.

function url_exists($url) {
    $hdrs = @get_headers($url);
    return is_array($hdrs) ? preg_match('/^HTTP\/\d+\.\d+\s+2\d\d\s+.*$/',$hdrs[0]) : false;
}

Source: http://us2.php.net/manual/en/function.file-exists.php#84918...

Share this snippet

 

Comments (1) - Leave yours

  1. David V. Wallin said:

    Nice function.
    My only real comment to this is that i, personally, hate ternary operation.

    I’d probably write it like this;

    function url_exists($url=false) {
    if ( $url != false )
    {
    $hdrs = @get_headers($url);
    if ( is_array($hdrs) )
    {
    return preg_match(‘/^HTTP\/\d+\.\d+\s+2\d\d\s+.*$/’,$hdrs[0]);
    }
    return false;
    }
    return false;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Please respect the following rules: No advertising, no spam, no keyword in name field. Thank you!