0

I'm goging to design benners and stuff for people and then I'll give them an image to put their profiles. I want to make a page on my localhost to check if image exist on their profile. Basically I want to see if "example1.com/myimg.jpg" exist on "example2.com/theirprofile".

I want to see if the image exists on an external page. I don't have much experience with php. Is it possible to do sometihng like this?

Deccoyi
  • 29
  • 1
  • 7
  • 1
    little thing called `file_exists()` http://php.net/manual/en/function.file-exists.php *"Checks whether a file or directory exists"* - Yet, if you're looking if it exists on an external site, then that's a different ballgame. – Funk Forty Niner Apr 04 '16 at 14:36
  • 1
    Possible duplicate of [PHP file\_exists directory](http://stackoverflow.com/questions/33311030/php-file-exists-directory) – Mihriban Minaz Apr 04 '16 at 14:42
  • @Fred-ii- Yep. It should be an external site :/ I found things for looking on local page. But can't find anything about external ones. – Deccoyi Apr 04 '16 at 14:54
  • You'd need to use CURL then. See this Q&A http://stackoverflow.com/questions/981954/how-can-one-check-to-see-if-a-remote-file-exists-using-php – Funk Forty Niner Apr 04 '16 at 14:59

1 Answers1

0

This is what I usually do:

The idea is to make a cURL request just to check the result. If it's 200 then the resource exists.

function externalResourceExists($url) {
    $ch = curl_init($url); //$url is the url you need to check
    curl_setopt($ch, CURLOPT_NOBODY, true); //Don't get the body, we don't need it.
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);                    
    curl_close($ch);        
    return $retcode == 200;
}

Then check if the function returns true.

apokryfos
  • 38,771
  • 9
  • 70
  • 114