title: php check if file exists on an external doman (accessing form a sub domain)
body: <p>I have a website on <a href="http://www.reelfilmlocations.co.uk" rel="nofollow">http://www.reelfilmlocations.co.uk</a></p>
  
  <p>The above site has an admin area where images are uploaded and different size copies created in subfolders of an uploads/images directory.</p>
  
  <p>I am creating a site for mobile devices, which will operate on a sub-domain, but use the database and images from the main domain, 
  <a href="http://2012.reelfilmlocations.co.uk" rel="nofollow">http://2012.reelfilmlocations.co.uk</a></p>
  
  <p>I want to be able to access the images that are on the parent domain, which i can do by linking to the image with the full domain i.e http:www.reelfilmlocations.co.uk/images/minidisplay/myimage.jpg</p>
  
  <p>Though i need to check if the image exists first...</p>
  
  <p>I have a php function that checks if the images exists, if it does it returns the full url of the image.</p>
  
  <p>If it doesn't exist i want to return the path of a placeholder image.</p>
  
  <p>The following function i have, returns the correct image if it exists, but if it doesn't, it is just returning the path to the directory where the placeholder image resides i.e <code>http://www.reelfilmlocations.co.uk/images/thumbs/</code>. without the no-image.jpg bit.</p>
  
  <p>The page in question is: <a href="http://2012.reelfilmlocations.co.uk/browse-unitbases/" rel="nofollow">http://2012.reelfilmlocations.co.uk/browse-unitbases/</a>
  the code i have on my page to get the image is:</p>
  
  <pre><code>&lt;img src="&lt;?php checkImageExists('/uploads/images/thumbs/', $row_rs_locations['image_ubs']);?&gt;"&gt;
  </code></pre>
  
  <p>My php function:</p>
  
  <pre><code>if(!function_exists("checkImageExists")){
      function checkImageExists($path, $file){
          $imageName = "http://www.reelfilmlocations.co.uk".$path.$file;
          $header_response = get_headers($imageName, 1);
          if(strpos($header_response[0], "404" ) !== false ){
              // NO FILE EXISTS
              $imageName = "http://www.reelfilmlocations.co.uk".$path."no-image.jpg"; 
          }else{
              // FILE EXISTS!!
              $imageName = "http://www.reelfilmlocations.co.uk".$path.$file;
          }
          echo($imageName);   
      }
  }
  </code></pre>
  
  <p>Failing to get this to work i did some digging around and read some posts about curl:</p>
  
  <p>This just returns a placeholder image everytime.</p>
  
  <pre><code>if(!function_exists("remoteFileExists")){
      function remoteFileExists($url) {
          $curl = curl_init($url);
  
          //don't fetch the actual page, you only want to check the connection is ok
          curl_setopt($curl, CURLOPT_NOBODY, true);
  
          //do request
          $result = curl_exec($curl);
  
          $ret = false;
  
          //if request did not fail
          if ($result !== false) {
              //if request was ok, check response code
              $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);  
              if ($statusCode == 200 ) {
                  $ret = true;   
              }
          }
          curl_close($curl);
  
          return $ret;
      }
  }
  
  if(!function_exists("checkImageExists")){
      function checkImageExists($path, $file){
          $imageName = "http://www.reelfilmlocations.co.uk".$path.$file;
  
          $exists = remoteFileExists($imageName);
          if ($exists){
              // file exists do nothing we already have the correct $imageName
          } else {
                      // file does not exist so set our image to the placeholder
              $imageName = "http://www.reelfilmlocations.co.uk".$path."no-image.jpg";   
          }
              echo($imageName);
      }
  }
  </code></pre>
  
  <p>I dont know if it could be to do with getting a 403, or how to check if this is the case.</p>
  
  <p>any pointers or things i could try woud be greatly appreciated.</p>