views:

175

answers:

2

I have an application that allows admins to upload files. These files are stored outside of the web root so they are not accessible via a URL. In the past, we have always used code similar to below to then serve the file back to authorized users. Is there a better or more universal way to specify the type? This is especially relevant when admins are allowed to upload many different types of files.

<cfheader name="content-disposition" value="filename=#queryname.filename#">
<cfcontent type="application/unknown" file="#application.pathToDataDirectory#/#queryname.filename#">
+6  A: 

As far as I know, i don't think that there is an actual "mime-type" written into any kind of file. It's just something that gives the browser a warning about what's coming its way.

Several "smart mime-type" functions are mentioned here (note the second comment as well). I have not tried these, but they should suit your needs: http://www.coldfusionmuse.com/index.cfm/2006/8/2/mime.types

If you wanted to write your own "smart mime-type" function, you could easily convert this PHP function into ColdFusion: http://snipplr.com/view/11451/get-file-mimetype/

jocull
we ended up recreating the PHP function, thanks
Jason
+1  A: 

Somewhat belated, but why don't you capture and save the MIME type when the file is uploaded? <cffile> returns it in cffile.contentType and cffile.contentSubType, so it's not exactly onerous. You'd need to run a sweep on pre-existing files, of course, but that's an easily scripted one-off.

FWIW, my file download code also sends file size and modified date, and checks for re-requests for unmodified files. All this after authentication/authorisation checks, of course:

<cfset modified=parsedatetime(queryname.datestamp)/>

<cfif structkeyexists(cgi, "http_if_modified_since")>
 <cfif parsedatetime(cgi.http_if_modified_since) gt modified>
  <cfheader statuscode=304 statustext="Not modified"/>
  <cfabort/>
 </cfif>
</cfif>

<cfheader name="Content-Disposition" value='disposition=#disposition#; filename="#queryname.filename#"'/>
<cfheader name="Content-Length" value=#queryname.size#/>
<cfheader name="Last-Modified" value=#gethttptimestring(modified)#/>
<cfcontent type=#queryname.mimetype# file="application.pathToDataDirectory/#queryname.filename#"/>
Pete Jordan