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.
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.
PHP Code:if (file_exists([imagename]) {
//do stuff here like
echo '<img src="imagename" alt="">';
}
Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
Currently: enjoying the early holidays :)Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide
Or, use the shorter (but more confusing) syntax of (condition?if:else).
It's a bit weird to deal with, but can make the code look much cleaner.Code:echo '<img src="'.(file_exists($img)?$img:$default).'">';
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
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.
Thou com'st in such a questionable shape
Hamlet, Act 1, Scene 4
(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.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Dan, are you in the business of giving people headaches?
What a masterpiece. Haha.
Thou com'st in such a questionable shape
Hamlet, Act 1, Scene 4
Actually, easier like this--
(isset($img)?(file_exists($img)?$img:@$default):@$default);
But, here's a bit of a rewrite that will help more--
That will check if the image is a valid image, not just if it exists as a file.Code:<?php $imgf = @$default; if (isset($img)) { if (@getimagesize($img)) { $imgf = $img; }} $img = $imgf; ?>
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
Bookmarks