php check if external image exists

Today small task for an ecommerce website is to look for all possible external images from supplier and download to our own server. We have thought of using @fopen() a common php function to open a file. However, there is more to find out.

< ?php
if (@fclose(@fopen("http://www.domain.com/image.jpg", "r"))) {
echo "External image exists";
} else {
echo "External image does not exist";
} ?>

However, we soon realised that using fopen() does not work properly if the external website handle 404 page well, which mean if the image does not exist their server will redirect audience to an “notfound” page or to “imagenotfound.jpg”.

To resolve this problem, you can simply use GetImageSize() function from GD library.

< ?php
if (@GetImageSize("http://www.domain.com/image.jpg")) {
<code>echo "External image exists";
} else {
echo "External image does not exist";
} ?>

Random Posts

This entry was posted in PHP, Web development and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">