Tag Archives: Check whether image exists on remote server

Check whether image exists on remote server

Here is example to verify whether given image exists on server or not. For that we can use PHP inbuilt function get_headers() .

get_headers — Fetches all the headers sent by the server in response to an HTTP request. get_headers function return an array with the headers sent by the server in response to a HTTP request.

<?php

$url = "http://blogs.ptutorial.com/wp-content/uploads/2015/08/logo_php5.png";

function checkImageExist($url){
$headers = get_headers($url, 1);
$status="";
if (strpos($headers['Content-Type'], 'image/') !== false) {
    $status=1;
} else {
    $status=0;
}   
return $status;
}
?>

The output of the get_headers function:

Array
(
    [0] => HTTP/1.1 200 OK
    [x-amz-id-2] => c/RpQuXgUvK8Fv6zMa2XIwjYfBMooqSmLBrbfyFRhfmEplvtZht0nI8iHxuQk7DysAFPlagkk6A=
    [x-amz-request-id] => A0FE2FA39A7860A3
    [Date] => Sat, 09 Feb 2019 05:17:40 GMT
    [Last-Modified] => Wed, 11 Jul 2018 20:16:46 GMT
    [ETag] => "7954ed8ca909c136bce3c762733df482"
    [Accept-Ranges] => bytes
    [Content-Type] => image/jpeg
    [Content-Length] => 37934
    [Server] => AmazonS3
    [Connection] => close
)