PHP is_file问题

时间:2015-08-08 19:15:41

标签: php

我使用以下代码:

$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

我做错了什么?

1 个答案:

答案 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';