Link to home
Start Free TrialLog in
Avatar of johnclarke
johnclarke

asked on

CFFILE and Tif Files

When I upload a TIF file via CFFILE I am informed that the mime type for this file is "Application/Octet-stream" rather than "images/tiff".  

This allows users to upload executables which is obviously a security risk.  We don't want this to happen.

Is there a solution to this problem ?

I would be grateful for all advice offered.

Thanks in advance

John
Avatar of cheekycj
cheekycj
Flag of United States of America image

detect the type of file it was and delete it if it isn't type tiff...

<CFSET uploadPath = "d:\inetpub\wwwroot\mywebsite\uploaddir\">
<CFLOCK timeout="60" throwontimeout="yes">
<cffile action="UPLOAD" filefield="#FORM.filename#" destination="#uploadPath#" nameconflict="MAKEUNIQUE" accept="application/octet-stream">
</CFLOCK>
<CFIF FILE.FileWasSaved>
   <CFIF File.ClientFileExt IS NOT "tiff">
       <CFLOCK timeout="60" throwontimeout="yes">
         <cffile action="DELETE" filefield="#FORM.filename#" destination="#uploadPath#">
       </CFLOCK>      
       Please upload TIFF Files only.
       File was not uploaded
   </CFIF>
</CFIF>

You can use JavaScript to check the extension before they click submit.

You can also do some parsing of the name to check the extension before you upload.

CJ
Avatar of johnclarke
johnclarke

ASKER

CJ,
   Thanks for your comment.  In the program we need to upload more than one type of file.  However, we wish to prevent a user from upload executables (eg PL,EXE, etc).

Users can change the file extension so we need a way to make CFFILE identify the correct mime type.

Thanks in advance

John
Well, the problem is that the mime-type of a file is dictated by the browser settings.. so I can change go and change my browser settings to make .exe files of type image or whatever.

CJ
Avatar of Scott Bennett
The best way to do this would be to have a javascript validation on the form and to also set the <cffile> tag to only accept the MIME type you want. The following example is set to accept gif's, bmp's, jpg's, and tif's.

NOTE: CJ had the same answer as this so If it works for you he should get the points. I just decided to give you some example code to look at since I had it handy.

-Scott





ImageUpload_1.cfm:

----------------------------------------------------------

<!--- This Javascript Validates the Image field --->
<script language="JavaScript1.2">

   
function Check_File_Field(){

         var TheValue = eval("document.ImageUpload.ImageFile.value");
         var FileType = TheValue.substr(TheValue.length-3, 3);
         
         if (TheValue == ""){
              alert("Please Select an Image to Upload");
         }
         
         
         if ( FileType == "gif" || FileType == "jpg" || FileType == "bmp" || FileType == "tif" ) {
                   document.ImageUpload.submit();
         }
         
         else{
              alert("The file is not a valid Image file.");
              eval("document.ImageUpload.ImageFile.focus()");
         }
    }
</script>
ImageUpload_1.cfm:

<!--- This form uploads the Image --->
<cfform name="ImageUpload" action="ImageUpload_2.cfm" enctype="multipart/form-data">
<input name="ImageFile" type="file">
<input type="Button" value="Upload Image" onclick="Check_File_Field()">
</cfform>

----------------------------------------------------------

ImageUpload_2.cfm:

-----------------------------------------------------------

<!--- If a file has been uploaded use cffile to check that it is am image file and store it in your
image folder --->
<cfif isDefined("Form.ImageFile")>
<CFFILE ACTION="upload"
   FILEFIELD="ImageFile"
   DESTINATION="C:\inetpub\wwwroot\ImageFolder"     NAMECONFLICT="MakeUnique"
    Accept="image/gif, image/pjpeg, image/bmp, image/tif"
    >

<!--- The following structure contains info on the file wich may be helpful for you to add the image
to your content management system --->

<cfset stFile = StructNew()>
<cfset stFile.FileName = "#file.serverfile#">
<cfset stFile.FilePath = "#file.serverdirectory#">
<cfset stFile.FileURL = "/ImageFolder/#file.serverfile#">

</cfif>

---------------------------------------------------------



"Users can change the file extension so we need a way to make CFFILE identify the correct mime type."

The mime type is entirely determined by the file extension anyway, so it really wont matter much.

Heath
ASKER CERTIFIED SOLUTION
Avatar of snakehollywood
snakehollywood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
johnclarke--->  You logged in recently, but did not update/finalize your open questions.  Please do.

If you've been helped, please accept the expert comment which helped you to grade and close it.  If you need help splitting points between multiple experts, comment with detail.

I will post this in all your open questions and monitor them for closure.  Please check the HELP DESK link on the left for site-related information on the Question/Answer process, Guidelines and Member Agreement.

Expert input is always welcome to determine the fair outcome of this question in the event johnclarke does not respond.

Thanks all,

Moondancer
Community Support Moderator @ Experts Exchange
IMHO split btw SBennett and me.

CJ
I find it odd that snakehollywood's comment was accepted as the answer for this question. In my opinion it was probably the least usefull comment on this whole string.

Scott
Well, I guess if it satisfied the question asker...

CJ
Thanks, johnclarke, for returning and updating/finalizing your questions.  Thank you also, CJ.

Moondancer
Community Support Moderator @ Experts Exchange