Page 1 of 1

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

Posted: Thu Jul 03, 2014 7:53 am
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?

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

Posted: Thu Jul 03, 2014 8:35 am
by Celauran
file_exists checks the local filesystem. Are these sites hosted on the same machine?

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

Posted: Thu Jul 03, 2014 9:04 am
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
}