PDA

View Full Version : Deleting Files Off The Server


Thatguy2001au
10-12-2002, 04:47 AM
HI

I hope somone can help me or just give me an idea on something.

My site enables people to upload advertisements and also pictures with those advertisements. The adverts are kept in a database but the images are in a folder on the server.

The site also allows the poster of the ad to delete their ad when they want. The problem is, i can delete the data from the database easily, but i am not sure of any commands i can use to delete a file which exists on the server.

Does anyone have any ideas?

Carl
10-12-2002, 06:15 AM
Yeah you can.


I haven't tested this but it should work.

set myfile=server.createobject("Scripting.FileSystemObject")
myfile.DeleteFile("filename")
set myfile=Nothing

carl

Mhtml
10-12-2002, 10:00 AM
Do you make a reference of the filename in the database when the image is uploaded? If not I think you should!

If so you can make it so that the person will not be able to delete a file othere than their own.

Then you can just do a query like this:

set DelConn = Server.CreateObject("ADODB.Connection")
DelConn.open "Driver={Microsoft Access Driver (*.mdb)}; DBQ="&server.MapPath("youdb.mdb")
Set rs = Server.CreateObject("ADODB.Recordset")
rs.open "SELECT * FROM TABLE_NAME WHERE USERID ="&Session("userId")
Conn.Execute(DelSql)

That way you can inplement easily into the FSO script Carl made!
set myfile=server.createobject("Scripting.FileSystemObject")
myfile.DeleteFile(rs.fields("picture_path"))
set myfile=Nothing
rs.close
set rs = nothing
DelConn.close
set DelConn = nothing


For that example you would need to have set a session variable called userId which would also have to be the name of a table in the db containig the user info.

Thatguy2001au
10-12-2002, 10:38 AM
Thanks to both of you!

Yes, i do make a reference of the image name in the database so it should make my job easier. I am sure i can get it to work now thanks to both of you.