views:

29

answers:

1

Hi guys, I'm building a simple email functionality in my web application and would like to set it up to be able to send in attachments as well. The thing is that I want to set up the ability to upload files i.e attachemnts asynchronously. I'd like to use SWFUpload for this however 'ajax' uploaders upload the file to a location on the server - that location is most often prearranged in code or in a config file. In this case I don't want to end up with a situation where concurrent users are uploading files onto this temporary area and they overwrite each other.

How do I set it up. I'm using php MYSQL and the prototype javascript library here.

+1  A: 

In this case I don't want to end up with a situation where concurrent users are uploading files onto this temporary area and they overwrite each other.

A good way to avoid this is to generate a random key before the upload process starts, and to prefix every uploaded file with that random key.

When it is time to send the E-Mail, you fetch all the files with that random key.

You could alternatively also create a temporary directory named after the random key, and store all the files there.

That way, you avoid collisions between users and even between multiple upload processes from the same user.

Pekka
Good idea - is it possible to use the session id however for each time someone wishes to do an upload. Is there any chance of the random key being duplicated [I might sound silly asking this but still]?
Ali
@Ali in theory, yes, although it is *extremely* unlikely. Still, to be safe, check whether such a directory / prefix already exists when creating the random key. Be careful with using the session ID - what happens if I have two browser windows open with the same session, and want to write two E-Mails at the same time?
Pekka
Ah gotcha . thanks for the help
Ali