Results 1 to 7 of 7

Thread: to check the image availability

  1. #1
    Join Date
    Jul 2007
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default to check the image 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.

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    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

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Or, use the shorter (but more confusing) syntax of (condition?if:else).

    Code:
    echo '<img src="'.(file_exists($img)?$img:$default).'">';
    It's a bit weird to deal with, but can make the code look much cleaner.
    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

  4. #4
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    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

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    (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

  6. #6
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    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

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Actually, easier like this--
    (isset($img)?(file_exists($img)?$img:@$default):@$default);


    But, here's a bit of a rewrite that will help more--
    Code:
    <?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.
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •