Saturday, February 13, 2010

PERL codes to check whether a image file Exists

Here are the codes I have been using. The key is to use the "-e" in the if statement. Other than that it is not a hard programming problem:


if (-e "../htdocs/prod/small/$image_id.jpg") {
$img_path = "/prod/small/$image_id.jpg";
}
else {
$img_path = "/img/default_small.gif";
}


Those in red (the relative path to the cgi-bin directory) depend on your server's configuration. One needs to ftp into the server to see the structure.

You can also "elsif" to support more file format rather than just jpg image files. Usually when an image file is not found, I will point it to a default "no photo" image file so that it looks more professional.

You then use the $img_path in the <img src=...> HTML tag.

Hope this helps.

No comments:

Post a Comment