我使用以下代码:
$s_img = 'logoDSK1-1423585418.jpg';
$slogo = is_file('http://www.domain.nl/members/images/logo/' . $s_img) ?
'http://domain.nl/images/logo/' . $s_img : false;
print $slogo ? $slogo : 'http://www.domain.nl/afbeeldingen/fotos/thumb_klein.jpg';
文件http://domain.nl/images/logo/logoDSK1-1423585418.jpg
存在
但每次代码返回时:http://www.domain.nl/afbeeldingen/fotos/thumb_klein.jpg
我做错了什么?
答案 0 :(得分:2)
这不是is_file
的工作原理。它告诉给定文件是否是常规文件。
我根据你的需要使用这个小家伙:
function remoteFileExists($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (curl_exec($ch)) return true;
else return false;
}
使用类似:
if (remoteFileExists('https://www.google.com/images/srpr/logo11w.png')){
echo 'Yay! Photo is there.';
} else {
echo 'Photo no home.';
}
或者,在您的情况下:
$slogo = remoteFileExists('http://www.domain.nl/members/images/logo/' . $s_img) ? 'http://domain.nl/images/logo/' . $s_img : false;
print $slogo ? $slogo : 'http://www.domain.nl/afbeeldingen/fotos/thumb_klein.jpg';