Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: php show image if exist

  1. #1

    php show image if exist

    using php how would i check to see if a image exist if so output it if not dont?

  2. #2
    PHP Code:
    <?php if (file_exists('filename.jpg')) echo '<img src="filename.jpg" alt="File" title="File" />'?>


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  3. #3

    Quote Originally Posted by nokrev
    PHP Code:
    <?php if (file_exists('filename.jpg')) echo '<img src="filename.jpg" alt="File" title="File" />'?>
    not working

  4. #4
    Err… that's descriptive.


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  5. #5
    ok what i am trying to do is show an image that sits inside a table and if there is no image then dont show the table.

    do i put the "file_exists" code in the query or before the table?

  6. #6
    Nokrev dont he need the \"


    so it would be

    <?php if (file_exists('filename.jpg')) echo '<img src=\"filename.jpg\" alt=\"File\" title=\"File\" />'; ?>
    Member #2 of the "I wont critique Timmytot's designs anymore" club.

  7. #7
    No. When using different style quotes (single vs. double), they're unnecessary. Additionally, double quotes should be used rarely in PHP, because they're much slower and attempt to parse variables. It's faster to concatenate a variable to a single-quote string than to have one string with an embedded variable.

    As for james' problem, you need to encapsulate the entire table inside the check for the image:

    PHP Code:
    <?php if (file_exists('image.jpg')) { ?>
    <table><stuff></stuff><stuff><img src="image.jpg" title="My image" alt="My image" /></stuff><stuff /></table>
    <?php ?>


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  8. #8
    Quote Originally Posted by nokrev
    No. When using different style quotes (single vs. double), they're unnecessary. Additionally, double quotes should be used rarely in PHP, because they're much slower and attempt to parse variables. It's faster to concatenate a variable to a single-quote string than to have one string with an embedded variable.

    As for james' problem, you need to encapsulate the entire table inside the check for the image:

    PHP Code:
    <?php if (file_exists('image.jpg')) { ?>
    <table><stuff></stuff><stuff><img src="image.jpg" title="My image" alt="My image" /></stuff><stuff /></table>
    <?php ?>
    Well either way would worlk, my way was correct, wasnt be all means the fastest way, but it would ahve done the job
    Member #2 of the "I wont critique Timmytot's designs anymore" club.

  9. #9
    Quote Originally Posted by Seb Hughes
    Well either way would worlk, my way was correct, wasnt be all means the fastest way, but it would ahve done the job
    You stated that you believed the escape slashes were necessary, which implies that it wasn't an alternative method and that it was a working method whereas nokrev's was 'not'. This could be compared to using:
    PHP Code:
    if(1+1==2){
        echo 
    "True";
    }

    //Instead of:

    if(((((((((((1+1))))))))))== 2){
        echo 
    "Also True";

    Both of these would work, however, it is probably obvious which one most people would suggest implementing in real life. If you had suggested your method before nokrev had, the 'alternate' method reason would have seemed more acceptable.
    “Who were you, Krilnon, and how did you know so much about AS4?”
    The historian sighed as she gazed up at the sky and saw… not stars. A story.

  10. #10
    Err… thanks for the long-winded explanation.


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  11. #11

    hi well it works in part but it hides both images. Here where it is going:
    PHP Code:
    <?php if (file_exists('$blogimg')) { ?>
                                        <table cellpadding="0" cellspacing="0" border="0">
                                            <tr>
                                                <td width="7">
                                                    <img src="images/frame_topleft.gif" width="7" height="7" alt="" border="0"><br>
                                                </td>
                                                <td width="10" background="images/frame_toptile.gif">
                                                    <img src="images/trans.gif" width="1" height="7" alt="" border="0"><br>
                                                </td>
                                                <td width="7">
                                                    <img src="images/frame_topright.gif" width="7" height="7" alt="" border="0"><br>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td width="7" background="images/frame_lefttile.gif">
                                                    <img src="images/trans.gif" width="1" height="1" alt="" border="0"><br>
                                                </td>
        <td width="10" bgcolor="#DDE9F4">
                                                    <img src="images/<?=$blogimg?>.jpg" width="450" height="150" alt="" border="0"><br>
        </td>
    <td width="7" background="images/frame_righttile.gif">
                                                    <img src="images/trans.gif" width="1" height="1" alt="" border="0"><br>
        </td>
        </tr>
        <tr>
        <td width="7">
            <img src="images/frame_botleft.gif" width="7" height="7" alt="" border="0"><br>
        </td>
        <td width="10"  background="images/frame_bottile.gif">
        <img src="images/trans.gif" width="1" height="1" alt="" border="0"><br>
        </td>
    <td width="7">
                                                    <img src="images/frame_botright.gif" width="7" height="7" alt="" border="0"><br>
    </td>
    </tr>
    </table>
    <?php ?>
    Last edited by james182; February 20th, 2006 at 03:15 AM.

  12. #12
    any idaes???

  13. #13
    70
    posts
    I love PHP & MySql too.
    PHP Code:
    <?php echo '<img src='images/$blogimg.jpg' width='450' height='150' alt='' border='0'>'?>

  14. #14
    After decifering your code, I think this is what you want:
    PHP Code:
    <?php 

    // blogimg should be something like this: 
    // imagename.jpg

    if (file_exists("images/".$blogimg)) { 
    ?>
                                        <table cellpadding="0" cellspacing="0" border="0">
                                            <tr>
                                                <td width="7">
                                                    <img src="images/frame_topleft.gif" width="7" height="7" alt="" border="0"><br>
                                                </td>
                                                <td width="10" background="images/frame_toptile.gif">
                                                    <img src="images/trans.gif" width="1" height="7" alt="" border="0"><br>
                                                </td>
                                                <td width="7">
                                                    <img src="images/frame_topright.gif" width="7" height="7" alt="" border="0"><br>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td width="7" background="images/frame_lefttile.gif">
                                                    <img src="images/trans.gif" width="1" height="1" alt="" border="0"><br>
                                                </td>
        <td width="10" bgcolor="#DDE9F4">
                                                    <img src="images/<?php echo $blogimg?>" width="450" height="150" alt="" border="0"><br>
        </td>
    <td width="7" background="images/frame_righttile.gif">
                                                    <img src="images/trans.gif" width="1" height="1" alt="" border="0"><br>
        </td>
        </tr>
        <tr>
        <td width="7">
            <img src="images/frame_botleft.gif" width="7" height="7" alt="" border="0"><br>
        </td>
        <td width="10"  background="images/frame_bottile.gif">
        <img src="images/trans.gif" width="1" height="1" alt="" border="0"><br>
        </td>
    <td width="7">
                                                    <img src="images/frame_botright.gif" width="7" height="7" alt="" border="0"><br>
    </td>
    </tr>
    </table>
    <?php ?>
    You definitally had it set up pretty screwy

    -Bigmtnskier
    Last edited by bigmtnskier; February 21st, 2006 at 12:28 AM.

  15. #15
    The real questions is… why all those nasty tables?


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012