Monday, 19 August 2013

php checking if file exists on an external domain always returns 403 or 404

php checking if file exists on an external domain always returns 403 or 404

ive been struggling with this for days now.
I have a subdomain mapped to a subfolder within the parent domain. i want
to use images images from its parent domain
i can link to the images fine by using a standard href in an image tag
linking to the file.
My problem is i need to runs some checks to see if the image exists and if
it does no to display an place holder image.
I have got all my code down onto a test page, the image tag displays the
image fine but any calls i do either using curl or get_headers returns 404
or 403 errors.
Both domains are on the same dedicated server under the same account and
they use the same credentials so its not a security issue (i checked with
my hosts)
SO why does the image display via html but i cant get the a 200 with php???
test page can be found at http://mobile.reelfilmlocations.co.uk/untitled.php
<?php
function remoteFileExists($url) {
//$url = str_replace(" ", '%20', $url);
$agent = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15';
$curl = curl_init($url);
//don't fetch the actual page, we only want to check the connection is ok
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_USERAGENT, $agent);
//do request
$result = curl_exec($curl);
echo('curl result: '.$result.'<br>');
$ret = false;
//if request did not fail
if ($result !== false) {
//if request was ok, check response code
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
echo('curl status: '.$statusCode.'<br>');
if ($statusCode == 200 ) {
$ret = true;
}
}
curl_close($curl);
return $ret;
}
function get_response_code($theURL) {
$headers = get_headers($theURL);
foreach ($headers as $key => $value){
echo "$key => $value\n";
}
return substr($headers[0], 9, 3);
}
$image1 =
'http://www.reelfilmlocations.co.uk/uploads/images/thumbs/no-image.jpg';
$image2 =
'http://www.reelfilmlocations.co.uk/uploads/images/thumbs/Boston%20Manor%20M4-9.jpg';
$image3 =
'http://www.reelfilmlocations.co.uk/uploads/images/thumbs/thisimagedoesnotexist.jpg';
?>
IMAGE 1: <?php echo($image1); ?> (this image does exist)<br /><br />
<?php echo(remoteFileExists($image1)); ?><br />
<?php echo(get_response_code($image1)); ?><br />
<img src='<?php echo($image1); ?>'/><br /><br />
IMAGE 2: <?php echo($image1); ?> (this image does exist)<br />
<br />
<?php echo(remoteFileExists($image2)); ?><br />
<?php echo(get_response_code($image2)); ?><br />
<img src='<?php echo($image2); ?>'/><br /><br />
IMAGE 2: <?php echo($image3); ?> (this image does not exist)<br />
<br />
<?php echo(remoteFileExists($image3)); ?><br />
<?php echo(get_response_code($image3)); ?><br />
<img src='<?php echo($image3); ?>'/><br />

No comments:

Post a Comment