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 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2004
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to check if a link exists

    for example how would I check whether an image URL exists or not?
    ie: http://someone-elses-site.com/flower.gif
    ??

  • #2
    adm
    adm is offline
    New to the CF scene
    Join Date
    May 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    saw this function somewhere - cant vouch for it but it seems to work.
    i.e
    Code:
    if (remote_file_exists('where.ever.com')){
    // yes
    }
    ---------------------------------------------------
    function remote_file_exists($url)
    {
       $head = '';
       $url_p = parse_url ($url);
    
       if (isset ($url_p['host']))
       { $host = $url_p['host']; }
       else
       {
           return '1 Invalid URL host';
       }
    
       if (isset ($url_p['path']))
       { $path = $url_p['path']; }
       else
       { $path = ''; }
    
       $fp = fsockopen ($host, 80, $errno, $errstr, 20);
       if (!$fp)
       { 
           return '2 Unable to connect to remote host';
       }
       else
       {
           $parse = parse_url($url);
           $host = $parse['host'];
    
           fputs($fp, 'HEAD '.$url." HTTP/1.1\r\n");
           fputs($fp, 'HOST: '.$host."\r\n");
           fputs($fp, "Connection: close\r\n\r\n");
           $headers = '';
           while (!feof ($fp))
           { $headers .= fgets ($fp, 128); }
       }
       fclose ($fp);
       
       // for debug
       //echo nl2br($headers);
       
       $arr_headers = explode("\n", $headers);
       if (isset ($arr_headers[0]))    {
           if(strpos ($arr_headers[0], '200') !== false)
           { return true; }
           if( (strpos ($arr_headers[0], '404') !== false) ||
               (strpos ($arr_headers[0], '410') !== false))
           { return false; }
           if( (strpos ($arr_headers[0], '301') !== false) ||
               (strpos ($arr_headers[0], '302') !== false))
           {
               preg_match("/Location:\s*(.+)\r/i", $headers, $matches);
               if(!isset($matches[1]))
                   return false;
               $nextloc = $matches[1];
               return remote_file_exists($nextloc);
           }
       }
       preg_match('/HTTP.*(\d\d\d.*)\r/i', $headers, $matches);
       return '3 Status Code not supported'.
           (isset($matches[1])?": $matches[1]":'');
    }

  • #3
    New Coder
    Join Date
    Apr 2004
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, but that method didn't work for me
    I ended up using a check for @getimagesize(), which worked pretty well.


  •  

    Posting Permissions

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