All -
I'm working on a personal Flash project that involves users uploading mp3s. For now, during my testing, I've got things working on my personal server. I'm using the FileReference Object, in conjunction with a bare bones PHP script
Right now, one of my challenges involve unique filenames. That is, if user A and user B both have a file on their machines named "song.mp3," and they both upload it... I would like each upload to have its own unique filename.
My current guess is that I could use something like Date.valueOf() to generate a unique name. Maybe this approach isn't 100% foolproof, but it's close enough for my needs.
Here's my question: how does this renaming occur? Can it be handle via Flash, or is this really more of a server/PHP question I'm asking here? I'm assuming uploading the file, and THEN renaming it sort of defeats the purpose... but I'm not clear if there's a way to handle this renaming stricly within Flash.
I'm in a bit over my head on this one, but enjoying the discovery process. Any thoughts/advice definitely appreciated!
Are you renaming ALL uploads? If not, you're going to need PHP to check what exists so having Flash rename sounds like a lot of unnecessary back and forth. I'm not a code genius like others here, but PHP is going to have to handle this one as it tries to save the file, so you won't be uploading then renaming. Your script will check to see if the name it is about to save is used then generate something else.
The only way you'd want Flash handling that is if you want to generate a random name for all uploads and pass that to PHP for it to save.
Storm -
Actually, yeah, I was planning on renaming all uploads. Rather than do a check to see if a filename exists, I figured having Flash generate a filename using Date.valueOf() might come fairly close to what I'm after.
This way, someone uploading a file titled "song.mp3" ends up uploading "1223577794055.mp3" instead.
Getting that filename to actually change is where I'm a bit lost. I assume that the FileReference "name" property refers to the filename on a user's computer (ie "song.mp3"). How that changes over to "1223577794055.mp3"... and whether it needs to be Flash or server-side, is what I'm unclear on.
You could always name the file by its MD5 hash (md5_file()). That way it's always unique, and any dupes don't matter as the file will be identical.
I think flash does not have much control over the file names remember there is a lot of security issues so flash is very much a transparent vessel with as little control as possible so reading file names is fine but I am not sure, I think the general idea is to run some server script that periodically renames and moves saved files from the default save area?
Well, if you don't mind renaming all files, and you want flash to know the new file name, you will have to generate the new name using flash and send it, most easily as a querystring appended to the upload, to the server-side script that saves it to your server.
Server script then saves and renames. Flash knows the new file name.
Thanks all. The querystring appended to the upload is a neat suggestion, and sounds like my best bet. I'll have to study up on my PHP and figure that part of it out.
Ultimately, my goal is to have the uploads point over to Amazon (I'm using their Simple Storage Service). I just figured I'd get the process down first using my own server... before tackling that next step, which is a bit much for me to get my head around.
Thanks for the advice all!
I actually just ran into this a while back. Let me see if I can recall what I did...
// edit:
I did it with two posts: one to upload the file, with the receiving script outputting the new filename. The second post then submitted the new filename along with the rest of the form info.
It wasn't pretty.