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

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

问题描述

我正在为图书 ISBN 生成图像的动态 URL.我需要一种可靠的 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);

    $result = curl_exec($ch);
    curl_close($ch);
    if($result !== FALSE)
    {
        return true;
    }
    else
    {
        return false;
    }
}

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

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

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

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