0

我正在跑步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

4

2 回答 2

3

问题是isImageFile需要基于 ColdFusion livedocs 的路径。您要做的是调用ImageNew,如果fileContent不是有效图像,它将引发异常。然后,您可以cfcatch该异常,然后采取相应的行为。如果它没有抛出异常,那么它就是一个有效的图像。

于 2012-08-10T15:25:11.737 回答
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>
于 2015-09-22T17:07:18.607 回答