PHP:检查图像是否存在(方法)

Ret*_*mus 2 php curl file-exists

我有 5000 多个图像链接,我需要找到一种方法来检查它们是否存在。我用过getimagesize(),但时间太长了。速度对我来说至关重要。

我写了一个小函数,但它不起作用,我不知道为什么。

function img_exists($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    if(curl_exec($ch))
        return true;
    else
        return false;

    curl_close($ch);
}
Run Code Online (Sandbox Code Playgroud)

目前我正在用 PHP 进行检查。请让我知道是否有更好的解决方案。

请注意,如果连接超时(1 秒),则该函数返回 false。速度很关键。

更新:文件位于不同的服务器上。

Ali*_*xel 5

如果可以,curl_multi_*函数应该使整个过程更快,请查看手册

此外,仅执行HEAD请求(而不是GET)将为您节省相当多的字节/时间,请添加以下内容:

curl_setopt_array($ch, array(CURLOPT_HEADER => true, CURLOPT_NOBODY => true));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
Run Code Online (Sandbox Code Playgroud)