#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    CF Builder path problem


    I am having problems with simple relative paths on code such as:

    <img src='../images/image309.png' />

    It works fine on the server, but running locally using CF Builder it can not find the images unless I include the entire path.

    This is the current directory:
    C:/ColdFusion9/wwwroot/New Folder/admin/

    I'm trying to display an image in:
    C:/ColdFusion9/wwwroot/New Folder/images/

    What am I missing or doing wrong. Sorry for such a basic question, but I'm new to CF.

    Thanks
  2. #2
  3. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,265
    Rep Power
    968
    This actually has nothing to do with CF, but is a common issue for web developers across the baord. Don't use relative paths for exactly this reason. Use an absolute path from the web root: "/New Folder/images/myimage.jpg".

    Or better yet, never reference a path at all. Set up an application variable in one place, such as:

    application.imagePath = "/New Folder/images";

    And then in your image tags use

    src="#application.imagePath#/myimage.jpg"

    That way if the structure ever changes you only have to alter one variable to make everything else work.
    Last edited by kiteless; May 19th, 2011 at 12:21 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0
    Thanks very much. That makes a lot of sense, but for some reason I still can not get it to work. I put this in application.cfm:
    <cfset application.imagePath = 'C:/ColdFusion9/wwwroot/New Folder/images/'>

    And I have this in another .cfm file:
    <cfoutput> #application.imagePath#image309.png </cfoutput> <br />

    <img src="#application.imagePath#image309.png" />

    --------------------------------
    The cfoutput displays:
    C:/ColdFusion9/wwwroot/New Folder/images/image309.png

    ...but the img tag still does not display the image.

    When I hard code the path, I need to use the full path, including the C:/... because anything less will not work.

    Do you have any idea what I am still doing wrong?

    Thanks
  6. #4
  7. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,265
    Rep Power
    968
    You can't use a system path. The browser has no idea what "C:\" is. It needs to be relative to the web server root. So if you view source and look at the image tag, it needs to look something like:

    "http://mysite.com/New Folder/images/myimage.jpg".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0
    I'm sorry to ask more questions, but my problem is running locally under Windows with ColdFusion Builder. My problem is not on the server, but keeping the same code working both locally and on the server.

    For some reason, locally I need to code the compete path, including c:. On the server I can use relative addressing such as ../images/test.jpg.

    Using CF Builder I am not sure how they implement the views they display when selecting IE or Firefox to test code, but the full system path is all I can get to work. CF Builder uses Apache server, if that matters.

    I don't see why your suggestion about application variables didn't work. The substitution seems OK as per my earlier post.

    Any other thoughts? I appreciate your help on such a trivial issue.
  10. #6
  11. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,265
    Rep Power
    968
    In order to run the CF code on your machine, you have to be running CF and a web server. Whether you're running it locally or on some other server doesn't matter. Nor does the fact that it is running on Windows, or the fact that you're launching the browser from CF Builder.

    When you run the page in your browser, what URL is in the location bar? Whatever that URL is contains your web root, which is probably http://localhost. Which is why you need to define the path from your web root to the image.

    If you specify the image source as "/New Folder/images/myimage.jpg", then that is the path from the web root that will be used by the browser to locate the image. Locally that could end up being "http://localhost/New Folder/images/myimage.jpg", and on a public web server it could be "http://www.mysite.com/New Folder/images/myimage.jpg". As you can see, it doesn't matter what the web root actually is. All that matters is that the path from the web root to your image is correct.

IMN logo majestic logo threadwatch logo seochat tools logo