【问题标题】:php check if file exists on an external doman (accessing form a sub domain)php 检查文件是否存在于外部域(从子域访问)
【发布时间】:2013-08-20 12:55:02
【问题描述】:

我在http://www.reelfilmlocations.co.uk有一个网站

上述网站有一个管理区域,用于上传图片,并在 uploads/images 目录的子文件夹中创建不同大小的副本。

我正在为移动设备创建一个站点,该站点将在子域上运行,但使用主域中的数据库和图像, http://2012.reelfilmlocations.co.uk

我希望能够访问父域上的图像,我可以通过链接到具有完整域的图像来做到这一点,即 http:www.reelfilmlocations.co.uk/images/minidisplay/myimage.jpg

虽然我需要先检查图像是否存在......

我有一个 php 函数来检查图像是否存在,如果存在则返回图像的完整 url。

如果它不存在,我想返回占位符图像的路径。

我有以下函数,如果存在则返回正确的图像,但如果不存在,它只是返回占位符图像所在目录的路径,即http://www.reelfilmlocations.co.uk/images/thumbs/。没有 no-image.jpg 位。

有问题的页面是:http://2012.reelfilmlocations.co.uk/browse-unitbases/ 我在页面上获取图像的代码是:

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

我的php函数:

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

未能让这个工作我做了一些挖掘并阅读了一些关于 curl 的帖子:

这只是每次都返回一个占位符图像。

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

我不知道这是否与获得 403 或如何检查是否是这种情况有关。

任何我可以尝试的指针或事情都将不胜感激。

【问题讨论】:

    标签: php curl get-headers


    【解决方案1】:

    我会使用 CURL,发出 HEAD 请求并检查响应代码。

    未经测试,但应该可以解决问题:

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

    上面的代码将使用申请的状态代码填充$res(注意我没有在$URL变量中包含http://前缀,因为我是在命令行中完成的。

    当然,使用 PHP 的 CURL 函数也可以达到同样的效果,并且上述调用可能无法在您的服务器上运行。我只是在解释如果我有同样的需要我会做什么。

    【讨论】:

    • 感谢您的回复,我认为主要问题是我将子域作为物理目录而不是作为父域中的文件夹,并且由于子域没有,我收到 403 错误有访问主域的权限,我可能会抓住稻草,我在主域上创建了一个新的子域作为文件夹,看看是否有帮助......
    • 如果图像应该在脚本的同一文件系统中,您可以简单地使用:file_exists()。 :)
    • Janio:事实上,这就是我在主域上使用的东西,它可以作为一种享受,尽管作为它的子域,我不能使用文件作为子域的外部 url我在...
    • 查看图片的相对路径即可:file_exists('subdomain/folder/image.png');。为什么它不适合你?
    • 因为子域不是主域中的文件夹,它作为一个单独的实体在物理上托管。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 2020-03-23
    相关资源
    最近更新 更多