If Image Exists - how does it work cross-domain ?

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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

If Image Exists - how does it work cross-domain ?

Post by simonmlewis »

Code: Select all

$thumbnail="http://www.site.co.uk/images/productphotos/small/$row->photoprimary";
if(file_exists($thumbnail))
{
  echo "<img src='http://www.site.co.uk/images/productphotos/small/$row->photoprimary' border='0' style='border: 1px solid #000000'/>";
}
else
{
  echo "<img src='/images/blank.gif' border='0' width='142px' style='border: 1px solid #000000'/>";
} 
We have a web site that uses images from one of our other web sites. That way, if we alter one, they all change.
However, there have been times when images are not on the other site for whatever reason. It thinks they are, so we get the 'x' box on the browser.
So I am trying to use the script above to query if the file exists - but it doesn't work. I think it's because of the way I am using $thumbnail as the target.

Where am I going wrong please?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: If Image Exists - how does it work cross-domain ?

Post by Celauran »

file_exists checks the local filesystem. Are these sites hosted on the same machine?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: If Image Exists - how does it work cross-domain ?

Post by simonmlewis »

I got the answer.

Code: Select all

$url = "http://www.site.co.uk/images/productphotos/small/$row->photoprimary";
$header_response = get_headers($url, 1);
if ( strpos( $header_response[0], "404" ) !== false )
{
// does not exist
}
else
{
// works
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply