Validate that Img URL exists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Validate that Img URL exists

Post by seodevhead »

Hey guys, I have a form where users can type-in an image URL that is later displayed in a post. Problem is, a lot of people put in a URL that does not actually point to any image, eventhough the URL is properly formatted. Is there anything I can do to ensure that the image URL that they enter actually returns a valid image and not just another "red X" in the post? Thanks for your help and advice. Take care.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Request the data via http.. With a head request you would get a response-code and eventually a content-type header.. If you really want to be sure make a get request and validate the content-type yourself...
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

Hey ... thanks for the tip... any idea what a good google query would be to find a tutorial on doing something like this? Thanks!!! :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I wrote this to read HTTP response headers... you can have it....

It should be fairly obvious what to do with it :)

Code: Select all

<?php

function get_http_headers($url)
{
    $ret = array();

    // Add the http protocol string if not there
    if (strtolower(substr($url, 0, 7)) != 'http://') $url = 'http://'.$url;
    if (preg_match('@^http://[^/]*$@i', $url)) $url .= '/'; //No path given, add one
    $url_parts = parse_url($url);
    
    $host = $url_parts['host'];
    $path = $url_parts['path'];
    
    $port = !empty($url_array['port']) ? $url_array['port'] : 80;
    $query = !empty ($url_array['query']) ? $url_array['query'] : '';

    $handle = @fsockopen($host, $port, $errno, $errstr); //Try to connect

    if (!$handle) return false; //Failed to connect

    //This is what a basic HTTP 1.1 request looks like
    // It *must* end with a blank line all by itself
    $GET_REQUEST = "GET $url?$query HTTP/1.1\r\n".
                   "HOST: $host\r\n".
                   //Spoof as Internet Explorer
                   "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)\r\n".
                   "Connection: close\r\n".
                   "\r\n";

    fwrite($handle, $GET_REQUEST); //Send our request
    do
    {
        $line = fgets($handle); //Read a line from the response
        $ret[] = trim($line);

    } //Keep reading until we hit a line by itself
    while (!feof($handle) && strpos($line, "\r\n", 0) != 0);

    fclose($handle);
    return $ret;
}

$headers = get_http_headers('http://www.google.com');
if (is_array($headers)) print_r($headers);
else echo "Failed\n";

/*
 Array
(
    [0] => HTTP/1.1 302 Found
    [1] => Location: http://www.google.co.uk/
    [2] => Set-Cookie: PREF=ID=e14339d040dc4e14:TM=1136249565:LM=1136249565:S=f87lu6EA38kICb4q; expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com
    [3] => Content-Type: text/html
    [4] => Server: GWS/2.1
    [5] => Content-Length: 224
    [6] => Date: Tue, 03 Jan 2006 00:52:45 GMT
    [7] =>
)
*/

?>
Post Reply