ColdFusion Muse

Ask A Muse: Cfinclude Versus Cffile

Mark Kruger December 6, 2006 1:24 PM Coldfusion Tips and Techniques Comments (6)

Muse Reader Asks:
"I am trying to use cfinclude to insert a txt file. But if the txt file has html / of cf code in it, i do not want it to be functional. can the txt file be read as a string?"

You know I love the easy ones. Your problem is misunderstanding the proper use of cfinclude. This tag passes the contents of the included file to the CF parser for execution. It's designed to compile into run time code. What you are trying to do is simply insert the contents of a file into the output buffer (the stuff that goes back to the browser). For that you can use cffile. Cffile will read the contents of your file into a variable - which you may then output onto the page. Here's an example.

<cffile     
    action="READ"
    file="#expandpath('./myfile.txt')#"
    variable="myFile"/>


<h4>output the contents of myfile</h4>

<cfoutput>#myfile#</cfoutput>

Notice the use of the expandPath() function. Unlike cfinclude which takes a relative Coldfusion path, cffile needs an absolute path. It needs something that looks like c:\inetpub\wwwroot\blah.txt (or maybe /var/home/www-data/blah.txt on a linux server). ExpandPath( ) is one of those can't-live-without-it functions that allows you to insert the path in a relative fashion.

In your question you indicated that you did not want the HTML to be "functional". I'm not sure what you mean by this - but I suspect you meant that you want the HTML to display on the page like "

this
" instead of like "this". For that you will need the htmleditformat( ) function as in...
#htmlEditformat(myfile)#
I hope this helps.

  • Share:

6 Comments

  • Ryan Guill's Gravatar
    Posted By
    Ryan Guill | 12/6/06 1:36 PM
    You can also just wrap the output in a <pre></pre> to get it to display instead of using htmleditformat(), if all you want to do is display it. It will even make it a fixed width font which is good for code and text files.
  • Peter Boughton's Gravatar
    Posted By
    Peter Boughton | 12/7/06 3:55 AM
    PRE just means pre-formatted; it will not prevent HTML tags from being rendered. (This is why there is the HtmlCodeFormat, which does the same as HtmlEditFormat but /also/ adds the PRE tags)

    There used to be an XMP tag which did this, but that died with v3, so shouldn't be used.


    The code I would recommend is: <pre>#XmlFormat(myfile)#</pre>


    Because XmlFormat escapes more than just the four characters which HtmlEditFormat converts. (importantly, it converts high ASCII characters)
  • Andy Jarrett's Gravatar
    Posted By
    Andy Jarrett | 12/7/06 5:25 AM
    Don't know the XMP tag. I haven't found a browser yet that doesnt support it, and is great when debugging. Though probably shouldnt rely on it :o)
  • Andy Jarrett's Gravatar
    Posted By
    Andy Jarrett | 12/7/06 5:26 AM
    My bad grammer is showing through. That first sentence should read "don't knock the XMP tag"
  • David's Gravatar
    Posted By
    David | 4/5/09 8:29 PM
    how do i enable cffile? i tried using your sample to see if it works, and it didnt, and i saw that i have to enable the tag in the CF administrator, but i cant find where. please help! =D
  • Mark Kruger's Gravatar
    Posted By
    Mark Kruger | 4/6/09 7:23 AM
    @david,

    You need to log into the Coldfusion admin and go to "security" in the naviagation menu.

    -Mark