我想检查是否存在远程映像。当前使用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);
执行HEAD请求并检查响应代码