gpt4 book ai didi

PHP最便宜的方法如何检查远程图像是否存在

转载 作者:行者123 更新时间:2023-12-02 18:00:26 25 4
gpt4 key购买 nike

我想检查远程图像是否存在。当前使用 CURL 并根据其对 i 的响应可以确定文件是否存在。然而,这也需要花费一些时间,具体取决于图像文件的大小。

有没有一种最安全、最便宜的方法来进行检查而无需离线或使用缓存?

$ch = curl_init();
// set URL and other appropriate options
/*--Note: if in sabre proxy, please activate the last 4 lines--*/
$options = array( CURLOPT_URL => $img,
CURLOPT_HEADER => false, // get the header
CURLOPT_NOBODY => true, // body not required
CURLOPT_RETURNTRANSFER => false, // get response as a string from curl_exec, not echo it
CURLOPT_FRESH_CONNECT => false, // don't use a cached version of the url
CURLOPT_FAILONERROR => true,
CURLOPT_TIMEOUT => 5
);


curl_setopt_array($ch, $options);

if(!curl_exec($ch)) { return FALSE; }

$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

return ($httpcode<400);

最佳答案

Perform a HEAD request并检查响应代码。

关于PHP最便宜的方法如何检查远程图像是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7631236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com