0

How can I check if a url directs to an image file, and http://website.com/images-stories/gallery/image-name.jpg

and if there is no file in the location, default to another url (for a default file).

I was using file_exists but realize that checks a path not a url.

<?PHP
    while ($data = mysqli_fetch_assoc($result)):

    $smaller_img = $data['smaller_img'];


    $url = $smaller_img;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, true);    // we want headers
    curl_setopt($ch, CURLOPT_NOBODY, true);    // we don't need body
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT,10);
    $output = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

if ($httpcode=200) 
{
$smaller_imgf=$url;
} else {
$smaller_imgf='../images/submit_icon.png';
}  
    ?>

<img src="<?php echo $smaller_imgf; ?>
rhill45
  • 559
  • 10
  • 35

1 Answers1

2

You can look with curl for status code 200

https://stackoverflow.com/a/11797723/2441442

If 200, you can check for image with getimagesize

http://www.php.net/manual/de/function.getimagesize.php

Community
  • 1
  • 1
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111