检查远程URL上是否存在图像 [英] Check whether image exists on remote URL

查看:139
本文介绍了检查远程URL上是否存在图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为图书ISBN生成图片的动态网址。我需要一个可靠的方法与PHP检查图像是否真正存在于远程URL。我尝试了各种方法与不同的PHP库,curl等,但没有一个很好,其中一些是彻底缓慢。鉴于我需要为我的数据库中的每本书生成(并检查!)约60个URL,这是一个巨大的等待时间。任何线索?

I am generating dynamic URLs of images for book ISBNs. I need a reliable way with PHP to check whether the images actually exist at the remote url. I tried various approaches with different PHP libraries, curl, etc., but none of them works well, some of them are downright slow. Given the fact that I need to generate (and check!) about 60 URLS for each book in my database, this is a huge waiting time. Any clues?

推荐答案

function checkRemoteFile($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    // don't download content
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if(curl_exec($ch)!==FALSE)
    {
        return true;
    }
    else
    {
        return false;
    }
}

- >这是最快的方法,如果你的主机支持curl

--> that is the fastest way if your host supports curl

这篇关于检查远程URL上是否存在图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆