0

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.

<img href="http://www.reelfilmlocations.com/images/myimage.jpg"/>

My problem is i need to run some checks to see if the image exists, if it does no to display a 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 response with php???

My 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 />
Dizzy Bryan High
  • 2,015
  • 9
  • 38
  • 61
  • This may help http://stackoverflow.com/questions/14699941/php-curl-check-for-file-existence-before-downloading – RiggsFolly Aug 19 '13 at 16:54
  • @Dizzy Bryan High What is `$ret` where do you handle this? – Phorce Aug 19 '13 at 16:57
  • $ret simply returns true or false i will do some checking code to check if it it a 200 and set $ret to true, ive stripped that out for now as i just want to understand why its returning 404... – Dizzy Bryan High Aug 19 '13 at 17:03
  • possible duplicate of [php check if file exists on an external doman (accessing form a sub domain)](http://stackoverflow.com/questions/18301744/php-check-if-file-exists-on-an-external-doman-accessing-form-a-sub-domain) – halfer Mar 14 '14 at 09:52

1 Answers1

1

You'd better show the placeholder with JS:

<img href="http://www.reelfilmlocations.com/images/myimage.jpg" onerror="this.src=PLACEHOLDER_URL"/>

Also, to specify the image path use "src" not "href"

<img src="http://www.reelfilmlocations.com/images/myimage.jpg" onerror="this.src=PLACEHOLDER_URL"/>
  • thats nice i like that, though as well as showing a place holder, theres some other functions i need to run such as checking if a hi-res image exists and if not to display a low res image, its a nice little work around but it does not answer my question – Dizzy Bryan High Aug 19 '13 at 17:15
  • 1
    I think the problem is with the web server. On your test page I can see the images but if I open the image directly in the browser I get a "404 - File or directory not found." – Daniel Cata Aug 19 '13 at 17:28
  • I've come to that conclusion too I had contacted support but they say it isn't, I have now sent them the links to the above pages, so hopefully they can resolve the problem. – Dizzy Bryan High Aug 20 '13 at 11:00