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:
I'm trying to display an image in:
What am I missing or doing wrong. Sorry for such a basic question, but I'm new to CF.
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
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 01:21 AM.
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:
...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?
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:
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.
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.