View Full Version : to check the image availability
sujith787
09-14-2007, 08:57 PM
i want to check wheater a image (jpg and gif) file exist or not. if exist i want to display the image else i want skip that image display.
how can i check the availability.
tech_support
09-15-2007, 04:27 AM
if (file_exists([imagename]) {
//do stuff here like
echo '<img src="imagename" alt="">';
}
djr33
09-15-2007, 01:26 PM
Or, use the shorter (but more confusing) syntax of (condition?if:else).
echo '<img src="'.(file_exists($img)?$img:$default).'">';
It's a bit weird to deal with, but can make the code look much cleaner.
alexjewell
09-15-2007, 08:43 PM
Of course, the above would require those 2 variables to be declared (just to clarify)...$img would equal the image you want to display if it exists and $default would be the image to display if $img doesn't exist.
djr33
09-16-2007, 12:06 AM
(isset($img)?(file_exists($img)?$img:(isset($default)?$default:'defaultstring')):(isset($default)?$default:'defaultstring'));
Fun. Ha.
Though, really, just using a string as $default makes more sense anyway, unless you have it in several locations.
Either way, I'd assume these would be defined somewhere, or the script would be pointless.
alexjewell
09-16-2007, 09:16 PM
Dan, are you in the business of giving people headaches? :p
What a masterpiece. Haha.
djr33
09-16-2007, 09:35 PM
Actually, easier like this--
(isset($img)?(file_exists($img)?$img:@$default):@$default);
But, here's a bit of a rewrite that will help more--
<?php
$imgf = @$default;
if (isset($img)) {
if (@getimagesize($img)) {
$imgf = $img;
}}
$img = $imgf;
?>
That will check if the image is a valid image, not just if it exists as a file.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.