coldfusioncfmlcffile

How can I confirm a cffile write is successful?


I am using cffile to create a new file or update an existing file, depending on what the user requested. The request comes through a form from the previous procedure, so the code involving cffile looks like this:

<cfset thefile = "#form.dyn#">
<cfoutput>
<cfsavecontent variable = "testvar">
  #form.editor1#
</cfsavecontent>     
<cffile action = "write"
    file   = "/var/www/reports/#thefile#.cfm"
    output = "#testvar#">
</cfoutput>

When I am done writing to the file, I want to confirm to the user that this happened. For a new file I could use IsDefined to check that it is there. But I can't think of a way to check for an existing file that was updated. I considered a try/catch on the cffile, but the catch operates only if nothing seems to go wrong. If I don't get an error on the catch, can I assume everything is all right? I would prefer a direct check if possible. Does anyone have an idea?


Solution

  • You could use cfdirectory with the action="list" and filter="your-filename" to get the following information about the uploaded file:

    If action = "list", cfdirectory returns the following result columns, which you can reference in a cfoutput tag:

    • name: Directory entry name. The entries "." and ".." are not returned.
    • directory: Directory that contains the entry.
    • size: Directory entry size.
    • type: File type: file, for a file; dir, for a directory.
    • dateLastModified: The date that an entry was last modified.
    • attributes: File attributes, if applicable.
    • mode: Empty column; retained for backward compatibility with ColdFusion 5 applications on UNIX.

    Of interest to you is the dateLastModified column.

    So you should be able to do something like:

    <cfdirectory action="list" name="dirQuery" directory="C:/var/www/reports/" filter="#thefile#.cfm">
    

    Then you can dump that result to see what information is available to you:

    <cfdump var="#dirQuery#">
    

    The dateLastModified column can be accessed like:

    <cfoutput>#dirQuery.dateLastModified#</cfoutput>