Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cffile delete multiple files

Status
Not open for further replies.

tina2

IS-IT--Management
May 1, 2001
169
US
I am designing a form that will allow users to upload and e-mail multiple attachments. I am having trouble deleting the files after they have been sent.

Here is what I have:

<!--- put the files on the server --->
<cfif FORM.attachment_1 neq &quot;&quot;>
<!--- first actually upload the file --->
<cffile action=&quot;upload&quot;
filefield=&quot;attachment_1&quot;
destination=&quot;c:\uploads\&quot;
nameconflict=&quot;Makeunique&quot;>
<!--- now create a temporary holder for the attachment later on --->
<cfset attachment_local_file_1 = &quot;c:\uploads\#file.serverfile#&quot;>
</cfif>
<cfif FORM.attachment_2 neq &quot;&quot;>
<cffile action=&quot;upload&quot;
filefield=&quot;attachment_2&quot;
destination=&quot;c:\uploads\&quot;
nameconflict=&quot;Makeunique&quot;>
<cfset attachment_local_file_2 = &quot;c:\uploads\#file.serverfile#&quot;>
</cfif>

<!--- Attach the files to an e-mail --->

<CFMAIL TO=&quot;me@mydomain.com&quot;

FROM=&quot;#qGetUser.Email#&quot;
<cfif FORM.attachment_1 neq &quot;&quot;>
<cfmailparam file=&quot;#attachment_local_file_1#&quot;>
</cfif>
<cfif FORM.attachment_2 neq &quot;&quot;>
<cfmailparam file=&quot;#attachment_local_file_2#&quot;>
</cfif>

************So far so good**************

<cffile action=&quot;delete&quot;
file = &quot;c:\uploads\#file.serverfile#&quot;>

This will delete one of the files. How can I get it to delete all of them? I will probably have up to 5 attachments....

Thanks!
Kristine
 
Instead of this:
<cffile action=&quot;delete&quot; file = &quot;c:\uploads\#file.serverfile#&quot;>

Do this:
<cffile action=&quot;delete&quot; file = &quot;#attachment_local_file_1#&quot;>
<cffile action=&quot;delete&quot; file = &quot;#attachment_local_file_2#&quot;>
 
DUH! I had tried that, but I left the Path in and got an error. [blush]
It worked like a charm this way!

Thanks Tooled!
 
File.ServerFile is only going to contain the name of the last file uploaded. I may be mistaken, but I think that <cffile> can only delete one file at a time, so you will need to loop through all of the files sent in the email (by name), or all of the files in the directory.

The easiest would be delete everything in the directory:

<!--- Do a CFDirectory to retrieve the files --->
<cfdirectory action=&quot;list&quot; directory=&quot;c:\uploads\&quot; name=&quot;FileList&quot;>
<!--- Loop through the directory and delete each file --->
<cfloop query=&quot;FileList&quot;>
<cffile action=&quot;delete&quot; file = &quot;c:\uploads\#Name#&quot;>
</cfloop>


Now, you should ONLY do this if you know that everything in the directory can be deleted. What happens if 2 emails are trying to be sent at the same time? One of them won't have and attachements (the first one deleted them). I don't know enough about your app to make that call, so let us know if you can't use this.


Hope This Helps!

Ecobb

&quot;Alright Brain, you don't like me, and I don't like you. But lets just do this, and I can get back to killing you with beer.&quot; - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top