Jump to content

Determine if a file exists


Fmdpa

Recommended Posts

I'm working on a user profile page that incorporates Gravatar. How can I detect if their avatar image does not exist? It appears to me that file_exists() is only for files in the server directories.

$key = md5(strtolower(trim($email)));if (!file_exists("http://www.gravatar.com/avatar/$key") {   echo 'No Gravatar found for this email address!';}

Link to comment
Share on other sites

  file_get_contents("http://example.com/image.jpg"); // get filelist($version,$status_code,$msg) = explode(' ',$http_response_header[0], 3); // $http_response_header is someting like: string(15) "HTTP/1.1 200 OK"if($status_code==200){echo "good work!";}else{echo "the url gives a ".$status_code."! ^please give an other url!";}

found it here: http://php.net/manual/en/reserved.variable...ponseheader.php(not tested)

Link to comment
Share on other sites

file_get_contents is unnecessary if you don't need anything from the body of the page. If the gravatar site does return a 404 for nonexistent gravatars, all you need are the headers. A GET request is unnecessary when a HEAD will do. You could use get_headers(). http://us.php.net/manual/en/function.get-headers.php

$headers = get_headers('http://foo');if (!strstr($headers[0], '404')) echo('found page');

But I'm curious, why do you need to check to see if a gravatar exists? Gravatar has a fallback option for avatars, such that you can select your own image as a fallback avatar.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...