You can send files through the code with a single line as follows:
<cfif isAuthorized> <cfcontent file="/path/to/files/outside/of/web/root/Form.pdf" type="application/pdf" reset="true" /> </cfif>
ColdFusion FTW, right.
Please note that processing large files (for example, 100 MB +) may cause some problems, since the files are transferred to RAM before sending. It seems that this is no longer the case, as Mike explains.
Another option is to use a content type like x-application if you want to force download.
UPD
You want to put this code in a file (say file.cfm) and use it for PDF links. Something like that:
<a href="file.cfm?filename=Xyz.pdf">Download file Xyz.pdf</a>
file.cfm:
<cfset basePath = "/path/to/files/outside/of/web/root/" /> <cfif isAuthorized AND StructKeyExists(url, "filename") AND FileExists(basePath & url.filename) AND isFile(basePath & url.filename) AND GetDirectoryFromPath(basePath & url.filename) EQ basePath> <cfcontent file="#basePath##url.filename#" type="application/pdf" reset="true" /> <cfelse> <cfoutput>File not found, or you are not authorized to see it</cfoutput> </cfif>
UPD2
Added GetDirectoryFromPath(basePath & url.filename) EQ basePath as a simple and quick protection against the mentioned security problem.
Personally, I usually use the ID / database approach, although this answer was originally intended as a simple guide, not a really comprehensive solution.
source share