小智 91
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;
}
}
Run Code Online (Sandbox Code Playgroud)
- >如果您的主机支持curl,这是最快的方法
小智 57
像这样使用getimagesize()方法
$external_link = ‘http://www.example.com/example.jpg’;
if (@getimagesize($external_link)) {
echo “image exists “;
} else {
echo “image does not exist “;
}
Run Code Online (Sandbox Code Playgroud)
这里没有"简单"的方法 - 您至少需要生成一个HEAD请求并检查生成的内容类型以确保它是一个图像.这没有考虑可能的推荐人问题.卷曲是去这里的方式.