Eroll Krasniqi Eroll Krasniqi - 21 days ago 5
PHP Question

PHP: Check if link is image and check if exists

i try to check if image exists and when i execute code say "Not Image".

if(@is_array(getimagesize("https://media.giphy.com/media/BvvBz8BnRqZOg/giphy.gif"))) {
echo "Work";
} else {
echo "Not Image";
}


Where is problem, Thanks in advance !

Answer

Try

$headers = get_headers('https://media.giphy.com/media/BvvBz8BnRqZOg/giphy.gif', 1);
if (strpos($headers['Content-Type'], 'image/') !== false) {
    echo "Work";
} else {
    echo "Not Image";
}   

You only need to check the headers to see it it is an image, $headers['Content-Type'] in the example is 'image/gif' which is caught by the if statement. For reference check out get headers in the docs http://php.net/manual/en/function.getallheaders.php