doudun8705 2012-01-19 12:30
浏览 55
已采纳

有没有办法检查远程图像是否存在? PHP

My site is running in LAMP, my image CDN is in nginx.

I want to do is: Check if a requested image has a copy in CDN server, if yes then loan the copy in cdn server, otherwise, load the local copy for user.

Is there a programmatically way to check whether the remote CDN image is exist?

(perhaps determine the header? as I notice that if request image isn't exist, it returns 404)

enter image description here

  • 写回答

5条回答 默认 最新

  • douxin9135 2012-01-19 12:34
    关注

    I use this method to ping distant files:

      /**
       * Use HTTP GET to ping an url
       *
       * /!\ Warning, the return value is always true, you must use === to test the response type too.
       * 
       * @param string $url
       * @return boolean true or the error message
       */
      public static function pingDistantFile($url)
      {
        $options = array(
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_URL => $url,
          CURLOPT_FAILONERROR => true, // HTTP code > 400 will throw curl error
        );
    
        $ch = curl_init();
        curl_setopt_array($ch, $options);
        $return = curl_exec($ch);
    
        if ($return === false)
        {
          return curl_error($ch);
        }
        else
        {
          return true;
        }
      }
    

    You can also use the HEAD method but maybe your CDN as disabled it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗