Question

I'm running Coldfusion8 and need to pick up and store images from a remote location and am trying to validate whether whats being picked is actually an image.

I usually do validation like this:

<cffile result="upload" action="upload" accept="image/png" filefield="Dateiname5" destination="#tempDirectory#" nameconflict="overwrite" />
    <cfset testFilePath = tempDirectory & upload.serverFile>
    <cfimage name="tempFile" action="read" source="#testFilePath#" />
    <cfif NOT isImageFile( testFilePath ) >
        <cfset fileDelete( testFilePath ) />
        <cfthrow type="FileNotFound" message="#tx_settings_icons_error_img#" />
    <cfelseif ImageGetHeight( tempFile ) NEQ 512 or ImageGetWidth( tempFile ) NEQ 512>
        <cfset fileDelete( testFilePath ) />
        <cfthrow type="FileNotFound" message="#tx_settings_icons_error_size#" />
    <cfelseif NOT listfindnocase(allow, upload.serverfileext) >
        <cfset fileDelete( testFilePath ) />
        <cfthrow type="FileNotFound" message="#tx_settings_icons_error_file#" />
    </cfif>
    <cfset fileDelete( testFilePath ) />

So I'm uploading to a secure folder, perform validation for type, dimensions and file extension and discard from my secure folder if any of the validations fail.

I now need to validate extension and type using cfttp and am not getting it to work.

Right now I have this:

<cfhttp timeout="45" throwonerror="false" url="#variables.testFilePath#" method="get" getasbinary="yes" result="variables.objGet">
<cfset varaibles.allow = "png,jpg,jpeg">
<cfset variables.objImage = ImageNew(variables.objGet.FileContent)>
<cfif NOT isImageFile( variables.objImage ) >
    <cfset variables.failedLoads = variables.failedLoads & img_paths.bilddateiname & "(" & tx_gen_filetype & "), ">
    <!--- <cffile action="delete" file="#variables.objImage#"> --->
<cfelseif listFindNoCase( variables.allow, variables.fileExt) EQ 0>
    <cfset variables.failedLoads = variables.failedLoads & img_paths.bilddateiname & "(" & tx_gen_fileformat & "), ">
    <!---  <cffile action="delete" file="#variables.objImage#"> --->
<cfelse>
    <cfdump output="e:\website\stokkers\test2012\dump.txt" label="catch" var="it's an image allright">
</cfif>

Questions:
Can I use isImageFile here at all or is the image I create not an image... I can check for? Also, if any of the validations fail, how to I delete the create image again (from memory I assume)? cffile action="delete" doesn't seem to work?

EDIT:
This is what I'm checking right now:

<cfhttp timeout="45" 
    throwonerror="false" 
    url="#variables.testFilePath#" 
    method="get" 
    useragent="Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12" 
    getasbinary="yes" 
    result="variables.objGet"
    >
<cfset variables.objImage = ImageNew(variables.objGet.FileContent)>
<!--- returns NO --->   
<cfdump output="e:\website\stokkers\test2012\dump.txt" label="catch" var="#IsBinary(variables.objImage)#">

<cfif NOT isImageFile( variables.objImage ) >
    <cfdump output="e:\website\stokkers\test2012\dump.txt" label="catch" var="not an image">    
    <!--- <cffile action="delete" file="#variables.objImage#"> --->
</cfif>

The file is a jpg, still I'm always failing isImageFile

Était-ce utile?

La solution

The problem is that isImageFile expects a path based on the ColdFusion livedocs. What you want to do is call ImageNew, and if the fileContent is not a valid image, it will throw an exception. You can then cfcatch that exception and then behave accordingly. If it does not throw an exception, then it is a valid image.

Autres conseils

The solution comes from a combination of Brian's and Dan's answers:

<cfparam name="form.fileUpload" default="">
<cfif len(trim(form.fileUpload))>
   <cftry>
      <cffile action="readBinary" 
            file="#form.fileUpload#" variable="aBinaryObj">
      <cfset myImage = ImageNew(aBinaryObj)>
      <cfset imgOkay = "Yes">

      <cfcatch type="any">
         <cfset imgOkay = "No">
      </cfcatch>
   </cftry>    

   <cfif imgOkay eq "Yes">
       <cffile action="upload" 
           fileField="fileUpload" destination="C:\docs">
       <p>Thank you, your file has been uploaded.</p>

   <cfelse>
      <p>We're sorry. The file chosen is not an image file,
         or it is corrupt, so it cannot be uploaded.</p>
   </cfif>
</cfif>

<form enctype="multipart/form-data" method="post">
  <input type="file" name="fileUpload" /><br /><br />
  <input type="submit" value="Upload File" />
</form>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top