1

我正在为我们的网站创建一个文件上传实用程序,如果上传格式无效(根据我们的规范,不值得在这里查看),我想删除 zip 文件解压缩到的文件夹及其所有内容。

到目前为止,我使用了一种创建动态批处理文件的方法,如下所示:

    <!--- check if folder exists before starting to delete --->
<cfif directoryexists("#file_path_course#")>

    <!--- this can be passed in a varaible or whatever ---> 
    <cfset tDirectory = "#file_path_course#"> 

    <!--- This is what we will put in the bat file ---> 
    <cfset tString ="RMDIR /S /Q " & tDirectory> 

    <!--- generate a .BAT file for later execution ---> 
    <cffile action="WRITE" file="#file_path_course#\delete.bat" output="#tString#">

    <!--- Now execute the file to delete everything (Folder and all sub-folders and files)---> 
    <cfexecute name="#file_path_course#\delete.bat" timeout="60"></cfexecute> 

    <!--- check if bat file exists --->
    <cfif fileexists("#file_path_course#\delete.bat")>

        <!--- now delete the bat file ---> 
        <cffile action="DELETE" file="#file_path_course#\delete.bat"> 

    </cfif>

    <!--- delete course folder --->
    <cfdirectory action="delete" directory="#file_path_course#" recurse="yes">

    <cfset course_files_deleted = "Yes">

</cfif>

但无可否认,我很担心 cfexecute 标签的允许使用。

还有另一个选项,它使用 cfdirectory 递归删除选项,它可以满足我的所有要求,但我想非常确定它不会删除我指向的文件夹之外的文件夹/文件。

还有第三种方式,它涉及一个 cfdirectory 并围绕它循环,但我也喜欢使用更少的代码行来执行简单操作的想法。

你最信任哪个选项?

我正在运行 IIS7、Coldfusion 8。

4

3 回答 3

6

为什么不直接使用cfdirectory?您说您担心它会删除您指定的文件夹“外部”的内容。它不会。就那么简单。如果是这样,那么标签将被破坏。:)

于 2010-09-07T16:50:07.770 回答
3

我没有编写批处理文件然后执行它,而是让 ColdFusion 完成所有工作。

<cfset targetDirectory = "C:\Websites\site\thisFolder" />
<cfif directoryExists(targetDirectory)>
<cfdirectory action="list" directory="#targetDirectory#" listInfo="" name="theseFiles" recurse="true" type="file" />
    <cfif theseFiles.recordcount gt 0>
    <cfloop query="theseFiles">
        <cffile action="delete" file="#targetDirectory#/#theseFiles.name#" />
    </cfloop>
    </cfif>
<cfdirectory action="delete" directory="#uploadDirectory#/#allFolders.name#" />
</cfif>
于 2010-09-07T16:52:33.380 回答
2

我要做的是将文件上传到 webroot 之外的临时目录。您可以使用 gettempdirectory() 来完成此操作,它使用系统的临时目录(c:\windows\temp for windows)

然后您可以将文件解压缩到临时目录的子目录中,并对解压缩的文件执行一些安全检查,并确保一切正常,同时不会打开您的站点以遭受任何攻击。如果一切顺利,您就可以将文件移动到它们最终的存放位置。如果没有,只需使用 cfdirectory(正如 cfjedimaster 指出的那样)删除子目录和所有文件。

于 2010-09-07T18:22:41.233 回答