Skip navigation
LeeBC
Currently Being Moderated

How to detect file not found?

Jul 19, 2007 12:52 PM

I want to display a photo but if it is not found, I want to display a generic stand-by photo. Is there a way in CF to determine if the image does not exist, leaving a broken image red "X" on the page?

Thanks,

Lee
 
Replies
  • Currently Being Moderated
    Jul 19, 2007 1:07 PM   in reply to LeeBC
    You need to think about how a web page and images relate to each other.
    ColdFusion does not handle the image, it handles the HTML that is sent
    to the client browser. The client browser then parses the html and sees
    an image tag. The browser then makes a new request to the server for
    the image and, in a default configuration, the web server would see that
    an image is not a ColdFusion template and just attempt to grab the image
    from the file system and return it to the client.


    So that gives you two options to attempt this functionality. One you
    can have CF check the file system {<cffile...> for the image before it
    builds the <img...> tag in the original request and send different
    <img...> tags based on that file check. This is very costly in IO and
    processing and would not scale well, but it is quick and easy to build.

    You can create an image.cfm file. You put this file into the 'scr'
    property of all your <img...> tags with a parameter of what image to
    show. Something like <img src="myImage.cfm?file="aPicture.jpg">. Then
    in the image.cfm file you then retrieve the requested image and return
    it using <cfheader...> tags and a <cfconent...> tag to properly return
    the binary image. This is still a fairly high IO process, but since it
    is isolated in separate requests, the user perception of the page
    loading is better then the first. Especially if you properly build you
    image tags with alt, width and height properties so that the browser can
    lay out the page while retrieving the images.
     
    |
    Mark as:
  • Currently Being Moderated
    Jul 19, 2007 1:30 PM   in reply to LeeBC
    <cfif fileexists(expandpath("./test.jpg")) IS "YES">
    Then display it
    <cfif>
     
    |
    Mark as:
  • Currently Being Moderated
    Jul 19, 2007 1:42 PM   in reply to LeeBC
    ColdFusion is not super efficient at file operations like this so I would opt for using Apache Mod_Rewrite to do this for you. You can use a RewriteCond operator to serve a default image if the called image does not exist. If you are using IIS then I am sure that ISAPI rewrite will have a similar function.
     
    |
    Mark as:
  • Currently Being Moderated
    Jul 23, 2007 10:44 AM   in reply to frank_tudor
    LeeBC wrote:
    > Thank you. Did exactly what I needed.

    Just be fully aware of the cost of that code. It will not scale well at
    all. This works fine an small applications serving only a few pages at
    at time. But, this kind of operation is not going to work well on an
    application serving thousands of pages a minute.

    There is nothing wrong with using it, just make sure one knows its
    limitations when one does use it.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points