Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6

Thread: Image Help

  1. #1
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    I need to run an if statment, to find if the image specified is actually there. I know what I'm doing to output the images, it's just determining whether the image is present that is causing me problems.

    So if "images/image.gif" was present it would output "images/image.gif", otherwise it would be "images/notfound.gif".

    Any Ideas?

    Thanks and have a great New Year all
    JR

  2.  

  3. #2
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    If it's on the local filesystem, use is_file(): http://us3.php.net/manual/en/function.is-file.php
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  4. #3
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    You could also test the validity of a filepointer (then it would work with external files as well) to see wether a file exists or not. If not, set the default image.

    Example:
    PHP Code:
    <body>
    <?php
    $img
    ="path/to/assumed/image.jpg";

    //open a file pointer
    @$fp=fopen("URL to file, or path/to/assumed/image.jpg"'r');

    //check to see if there is a file there
    if(!$fp)
     {
      
    $img="path/to/default image.jpg";
     }
     
    fclose($fp);
    ?>
    <img src="<?php echo $img ?>">
    </body>
    Maybe it's clumsy, but it would work.
    S. Rosland

  5. #4
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    yikes rosland.. that'd take up major server resources.

    Just use:
    Code:
    if (is_readable('images/image.gif')){
    blah blah blah
    }
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  6. #5
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Would it?

    I might be wrong, but the fopen() command doesn't actually read the file. It just opens a path to it! (as far as I've understood). Meaning, no data is transferred before you actually use the filepointer as an argument in a setting like fread() or something.

    I tried it on a standalone PC, both with a valid file and a nonexisting file. In both cases, the request made zippo delays. The pointer is closed the second the script has validated the pointer, so it doesn't hold on to any memory resources.

    If I, on the other hand, tried to actually use the filepointer (using fgets or fread) then everything got quite busy for a while.

    So, unless proven differently, I think the fopen command just prepares a path to the file.
    Test it yourself. Point to a huge file (2-300MB) and a small file (a couple of KB) and see if you notice any performance difference!

    EDIT:
    Then of course, your example is more elegant! I wasn't aware of that function. I'll test it out as soon as I can. :classic:
    S. Rosland

  7. #6
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    Thanks all for your input
    JR


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 07:43 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com