php check if file exists on an external doman (accessing form a sub domain)

纵然是瞬间 提交于 2019-12-12 17:24:58

问题


I have a website on http://www.reelfilmlocations.co.uk

The above site has an admin area where images are uploaded and different size copies created in subfolders of an uploads/images directory.

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, http://2012.reelfilmlocations.co.uk

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

Though i need to check if the image exists first...

I have a php function that checks if the images exists, if it does it returns the full url of the image.

If it doesn't exist i want to return the path of a placeholder image.

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 http://www.reelfilmlocations.co.uk/images/thumbs/. without the no-image.jpg bit.

The page in question is: http://2012.reelfilmlocations.co.uk/browse-unitbases/ the code i have on my page to get the image is:

<img src="<?php checkImageExists('/uploads/images/thumbs/', $row_rs_locations['image_ubs']);?>">

My php function:

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);   
    }
}

Failing to get this to work i did some digging around and read some posts about curl:

This just returns a placeholder image everytime.

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);
    }
}

I dont know if it could be to do with getting a 403, or how to check if this is the case.

any pointers or things i could try woud be greatly appreciated.


回答1:


I'd do it using CURL, issuing a HEAD request and checking the response code.

Not tested, but should do the trick:

 $URL = 'sub.domain.com/image.jpg';
 $res = `curl  -s -o /dev/null -IL -w "%{http_code}" http://$URL`;
 if ($res == '200')
     echo 'Image exists';

The code above will populate $res with status code of the requisition (pay attention that I DON'T include the http:// prefix to the $URL variable because I do it in the command line.

Of course the same might be obtained using PHP's CURL functions, and the above call might not work on your server. I am just explaining what I'd be doing if I had the same need.



来源:https://stackoverflow.com/questions/18301744/php-check-if-file-exists-on-an-external-doman-accessing-form-a-sub-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!