如何在cfhttp上传的Coldfusion中验证和删除图像?

时间:2012-08-10 13:15:09

标签: validation coldfusion cfhttp

我正在运行Coldfusion8并需要从远程位置获取并存储图像,并且我正在尝试验证所选的是否实际上是图像。

我通常会这样做验证:

<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 ) />

所以我上传到安全文件夹,对类型,尺寸和文件扩展名执行验证,如果任何验证失败,请从我的安全文件夹中丢弃。

我现在需要使用cfttp验证扩展名和类型,并且我没有让它工作。

现在我有这个:

<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>

问题:
我可以在这里使用isImageFile或者我创建的图像不是图像......我可以查看吗?此外,如果任何验证失败,如何再次删除创建映像(从我假设的内存)? cffile action="delete"似乎不起作用?

修改
这就是我现在正在检查的内容:

<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>

该文件是jpg,但我总是失败isImageFile

2 个答案:

答案 0 :(得分:3)

问题是isImageFile期望基于ColdFusion的liveocs路径。您要做的是调用ImageNew,如果fileContent不是有效图像,则会抛出异常。然后,您可以cfcatch该异常,然后相应地执行操作。如果它没有抛出异常,那么它就是一个有效的图像。

答案 1 :(得分:0)

解决方案来自Brian和Dan的答案:

<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>