php check if external image exists
November 17th, 2009
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"; } ?>
Categories: PHP, Web development | Tags: external image, fopen, GD, GetImageSize | No Comments



