D F
asked on
Mass delete files from server?
I'm letting my members update multi images at the same time using a form. If I wanted to let them select the images they wanted to delete, how do I delete the files off the server. Do I use some form of a loop. I'm using cffile action for one but don't know how to do more then one at the same time??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry for the delayed response.
Almost. To delete multiple files, you'd need to run a query first so you can retrieve the physical file names. Once you have the query, loop through it and delete the files. Finally, delete the db records.
I don't know all of your column names, but it'd be something like this (not tested)
Almost. To delete multiple files, you'd need to run a query first so you can retrieve the physical file names. Once you have the query, loop through it and delete the files. Finally, delete the db records.
I don't know all of your column names, but it'd be something like this (not tested)
<cfparam name="form.deleteImage" default="">
<!--- if there's something to delete ....--->
<cfif listLen(form.deleteImage)>
<!--- first get names of the files to be deleted ---->
<cfquery name="getImagesToDelete" datasource="#dns#">
SELECT ID, TheFileName
FROM photos
WHERE ID IN (
<cfqueryparam value="#form.deleteImage#" list="true" cfsqltype="cf_sql_integer">
)
</cfquery>
<!--- next delete the image files on disk ---->
<cfloop query="getImagesToDelete">
<cfset pathToImage = "#mainsitepath#\images\photos\thumbs\#TheFileName#">
<cfif FileExists(pathToImage)>
<cffile action="delete" file="#pathToImage#">
</cfif>
</cfloop>
<!--- finally, delete the image records ---->
<cfquery datasource="#dns#">
DELETE FROM photos
WHERE ID IN
(
<cfqueryparam value="#form.deleteImage#" cfsqltype="cf_sql_integer" list="true">
)
</cfquery>
</cfif>
ASKER
Here is what I'm using now, is this correct?
Thank you for your help
Open in new window