Muse Reader Asks:
To use cffile locally, I need to specify a filepath like c:\cfusionmx\wwwroot\uploaded_files for it to work. But if i upload it to the server, this filepath won't exist. Do i just put something like file="news.txt" or specify a URL?
This question is an excellent reminder to folks like me that there are many burgeoning ColdFusion developers out there that need some help with the fundamentals. I often write about complex tuning or obscure behaviors (with the server, not that thing I have about Jell-O and a duck). But sometimes us CF Bloggers need to go back and produce material for developers who are just starting out.
Your problem here, dear reader, is that you are working in a dual environment - testing code on your desktop and deploying it to the server. That's a very good thing. In fact, I applaud you for it since most folks start out the other way around, making changes to production server code and only finding out it is a bad idea right after they create their first infinite loop. In fact there are some nuances to a dual environment that you have to think about ahead of time. In general, an application of any size will need some "global variables" that are specific to its hosted environment. That's where the Application scope usually comes in - but we can save that post for a follow up. Right now let's tackle your specific issue.
There's a little function called "expandpath()" that is going to solve your dilemma quite nicely. Basically, it takes a snippet of "relative" path and uses it to create a physical path relative to where the script running it is located. That may sound daunting, but you are probably already doing something like this with images or css or JavaScript (as in src="../images/logo.jpg"). Here are some samples. In each sample I'm setting a variable called "fPath" to a physical location.
In your example you have a directory called "uploaded_files" that is inside of the wwwroot folder. So what I really need is to find the directory that is "one up" from my "/content" directory. I need to move back up the chain. That looks like this.
Ok, so we have a directory that is the root (to take our second example). How do we use that with our CFFILE operation? It turns out that CFFILE takes a string as an argument for the "file" attribute (actually for all of it's arguments). So all we need to do is get our path looking right and we are done.
I hope this helps our reader. Keep those questions coming.