Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Show image if its available...

    I need a script that will allow me to show an image, but if that image doesn't exists, then show a default image. Here's my scenario, I have an upload script where the user uploads a picture, now I need a script that will show that picture if it actually exists (meaning it was uploaded), if it doesn't exist, then I need it to show a "default" picture.

    So basically:

    Image show "/path/to/Picture1.jpg", but if Picture1.jpg doesn't exists, then show "domain.com/default.jpg"

    Sorry, I just dont really know how to explain this..

  • #2
    New to the CF scene
    Join Date
    Jul 2010
    Location
    Sofia, Bulgaria
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can use php's file_exists function to check if the file exists and display default.jpg if it doesn't.

    Other approach is to use .htaccess in your image path and substitute the 404 error page with your default.jpg.

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    St George, UT
    Posts
    138
    Thanks
    6
    Thanked 17 Times in 17 Posts
    I did this for one of my projects:

    PHP Code:
            if (is_file($WRCMS['doc_root'].'/cooks/avatars/'.$owner_id.'-avatar-thumb.png')) {
                
    $avatar '<img src="'.$WRCMS['root'].'/cooks/avatars/'.$owner_id.'-avatar-thumb.png" width=32 title="avatar" alt="avatar">&nbsp;';
            }
            else {
                
    $avatar '<img src="'.$WRCMS['root'].'/cooks/avatars/default_avatar.png" height=32 width=32 title="default avatar" alt="default avatar">&nbsp;&nbsp;';
            } 

  • Users who have thanked wildreason for this post:

    adominator101 (08-02-2010)

  • #4
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Guys


  •  

    Posting Permissions

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