2012-05-07 4 views
0

존재 여부를 확인 나는 페이지가 실제로 존재하는지 확인하기 위해이 멋진 방법을 발견 정말 이미지 만. 예를 들어 다음과 같은 링크를 시도하면 www.google.com/image.jpg Google에 자체 오류 페이지가 있기 때문에 200을 반환합니다. 그러나 이미지가 없음을 알 수있을뿐만 아니라 그 페이지에 다른 이미지 (예 : http://www.kapstadt-entdecken.de/wp-content/gallery/robben-island/1robben-island.jpg)가있을 때에도 해당 페이지의 이미지 만있는 것은 아닙니다.PHP/JQuery와 URL의 이미지가 정말

어떻게해야합니까?

고마워요!

데니스

get_headers와도
+1

확인 콘텐츠 형식을 – Musa

+0

다른 내용은 무엇입니까? –

답변

1

image/png 
image/jpeg 
image/gif 

될 것입니다 기본적으로 그렇게하지 않습니다) :

stream_context_set_default(array(
    'http' => array(
     'method' => 'HEAD' 
    ) 
)); 

둘째, 당신은 당신을 위해 그것을 구문 분석 get_headers에 두 번째 매개 변수를 전달할 수 있습니다

$headers = get_headers($imageurl, 1); 

를 그리고, 당신은 정상에 따라 나머지 확인할 수 있습니다

if (strpos($headers[0], '200') === false) { 
    echo "not valid"; 
    exit; 
} 

if (isset($headers['Content-Type']) && 0 === strncmp($headers['Content-Type'], 'image/', 6)) { 
    echo "valid image"; 
} else { 
    echo "probably not an image"; 
} 
+0

나는 그것이 작동하지 않는지 검사 만하면된다. 그래서 if (! isset ($ headers [ 'Content-Type']) && 0 === strncmp ($ headers [ 'Content-Type'] , 'image /', 6)) { echo "not valid"; exit; } 불행히도 작동하지 않습니다 ... – weltschmerz

+0

반대는'! isset ($ headers [ 'Content-Type']) || 0! == strncmp ($ 헤더 [ 'Content-Type'], 'image /', 6)' –

+0

아니요. 그것은 매력처럼 작동합니다. 이 반비례는 분명히 다음과 같이 작동합니다. if (! isset ($ headers [ 'Content-Type']) && 0 === strncmp ($ headers [ 'Content-Type'], 'image /', 6)) – weltschmerz

0
$headers = @get_headers($imageurl); 

$is_image = false; 
foreach ($headers as $header) { 
    if (strpos($header, 'Content-Type: image/') === 0) { 
     $is_image = true; 
     break; 
    } 
} 
0

... 그 중 하나는 그래서 당신은 아마 (헤더를 가져올 때 HEAD 요청을 사용하는 것이 좋습니다

$isImage = false; 
foreach($headers as $header){ 
    if(strpos($header,'image/')==true){ 
     $isImage = true; 
    } 
}