php 检查远程 URL 上是否存在图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1363925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:12:33  来源:igfitidea点击:

Check whether image exists on remote URL

phpurlimagecurl

提问by Cristian Cotovan

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?

我正在为图书 ISBN 生成图像的动态 URL。我需要一种可靠的 PHP 方法来检查图像是否确实存在于远程 url 中。我尝试了使用不同 PHP 库、curl 等的各种方法,但没有一个效果很好,其中一些非常慢。鉴于我需要为数据库中的每本书生成(并检查!)大约 60 个 URL,这是一个巨大的等待时间。有什么线索吗?

回答by dangkhoaweb

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

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

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

回答by mohsin139

Use getimagesize() method like this

像这样使用 getimagesize() 方法

$external_link = ‘http://www.example.com/example.jpg';
if (@getimagesize($external_link)) {
echo  “image exists “;
} else {
echo  “image does not exist “;
}

回答by ChssPly76

There is no "easy" way here - at a very minimum, you need to generate a HEADrequest and check the resulting content type to make sure it's an image. That's not taking into account possible referrer issues. curl is the way to go here.

这里没有“简单”的方法——至少,您需要生成一个HEAD请求并检查生成的内容类型以确保它是一个图像。这没有考虑可能的推荐人问题。curl 是这里的方法。

回答by Andrew Deal

I have been doing this for my real estate picture tracking...

我一直在这样做是为了我的房地产图片跟踪...

$im = @imagecreatefromjpeg($pathtoimg);
if($im)
  imagedestroy($im); // dont save, just ack...
elseif(!$missing[$inum])
  $img404arr[] = $inum;

It 'seems' faster than downloading the actual image, taking about .3 seconds for each from images that avg 100k.

它“似乎”比下载实际图像要快,从平均 100k 的图像中下载每个图像大约需要 0.3 秒。

I wish I could just do a header check and read whether I get a 200 vs a 404 without downloading anything. Anyone have that handy?

我希望我可以只做一个标题检查并阅读我是否得到 200 与 404 而不下载任何东西。有谁用得着吗?

回答by Kevin Peno

You could use curl. Just set the curl option CURLOPT_NOBODY to true. This will skip body information and only get the head (thus http code as well). Then, you could use the CURLOPT_FAILONERROR to turn this whole process into a true/false type check

你可以使用curl。只需将 curl 选项 CURLOPT_NOBODY 设置为 true。这将跳过正文信息并只获取头部(因此也是 http 代码)。然后,您可以使用 CURLOPT_FAILONERROR 将整个过程转换为真/假类型检查

回答by ChronoFish

It's probably moot at this point, but this works for me:

在这一点上可能没有实际意义,但这对我有用:

function is_webfile($webfile)
{
 $fp = @fopen($webfile, "r");
 if ($fp !== false)
  fclose($fp);

 return($fp);
}

回答by Gr Brainstorm

Solution from https://www.experts-exchange.com

来自https://www.experts-exchange.com 的解决方案

<?php
function url_exists($url) {
    if (!$fp = curl_init($url)) return false;
    return true;
}
?>

回答by kheengz

if(@getimagesize($remoteImageURL)){
    //image exists!
}else{
    //image does not exist.
}

回答by Jaimal Chohan

If the images all exist on the same remote server (or in the same network), you could run a web service on that server that will check the file system for the the image file and return a bool value indicating wheter the image exists or not.

如果图像都存在于同一个远程服务器(或在同一个网络中),您可以在该服务器上运行一个 Web 服务,该服务将检查图像文件的文件系统并返回一个布尔值,指示图像是否存在.