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 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2006
    Posts
    66
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Coding for 404 images..

    I have a set of images in my /img folder... for example:

    rick_james.jpg
    michael_jackson.jpg
    barbara_streisand.jpg
    prince.jpg
    stevie_ray_vaughn.jpg

    I have a script that uses a variable $name that displays the images listed above.

    PHP Code:
    <img src="/img/<?php echo $name?>.gif">
    When $name = rick_james, michael_jackson, etc... all is good and it shows the appropriate picture.

    BUT say if the variable $name = kenny_loggins, then it throws a 404, saying img/kenny_loggins.gif does not exist.

    What is happening is that my server is working overtime trying to display images that do not exist.

    How can I write code that fails properly?

    in other words, if the image does not exist, don't try to look for it, if it does then go ahead and display it.

    Thanks!

  • #2
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    You're going to have to run some authentication of the submitted variable. You can do so in multiple ways, but one suggestion is to try something like:

    PHP Code:
    if(!is_file($name.".gif")) // error
    else // display image 
    You will, of course, need to ensure the proper pathing to check for the file's existence (if you check the wrong folder, you'll always return no-file). Those details can be seen at php.net by looking up the is_file function.

    Hope that helps.

  • #3
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Or use an array...

    PHP Code:
    $images = array(
        
    "rick_james.jpg",
        
    "michael_jackson.jpg",
        .
        .
        .
    );
    if(!
    in_array($name$images)) {
        
    $name "no-pic";

    or even a switch / case statement


  •  

    Posting Permissions

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