I'd like to know if you guys could suggest me the best way to structure file uploads.
I'm making a form for a site and this form contains an image upload section. In this section, images are uploaded before the form is even submitted (via iframe). Since the form hasn't been submitted yet, I have no DB record to link the images to so what I do is on every upload I insert an im开发者_StackOverflow中文版age token to the form. When the form submits, I loop with through the tokens and link them to the newly created DB record.
Might be important:
Images might be uploaded but the user might discard them before submitting, thus leaving a unused record in the DB and a unused image in the system. I'd have to clear unused files every so often.
I'm somewhat afraid of images somehow losing their record and getting 'lost' in the system. I'm probably behind paranoid though.
What do you guys think?
EDIT: I forgot to mention but I create a thumbnail version of the image to go along with the original. Should I just append "_thumb" to the file name and put it wherever the original is? Should the thumbnail version have its own database record? Thanks in advance.
I do this.
Lest say you have a "posting" section. Each time an user enters there I create a token, that token is sent as a hidden field when the image its uploaded, then I save the image on a temporal folder where files are accessible by the document root for my webserver. Along that I add to each file a timestamp, so an image could be named like this
<token>_<timestamp>.jpg
once the image its uploaded I reply with the image name and save it on a hidden value.
This way users can see/delete/etc the images and then you make the hard work once the form its submitted.
To clean images from the temp directory I just iterate it each several hours and use glob get the oldest and then delete them.
You don't commit anything to your database until you transferred the uploaded file from the temporary folder to the final destination, be it in the filesystem or a database. This way you don't end up with "orphan" records.
Edit:
You can save image metadata, including all images and thumbnails (as blobs) in the database. You can also keep images in the filesystem and save just the metadata in the database. It is really up to you if you want to save different versions of each image (wiki-style). This allows a user to rollback to a prior version. You can also simply overwrite any previous version of the image, which can save a lot of disk space. Anything that has a _thumb
suffix can automatically be associated with a parent image. I don't see that as an issue.
I'd have to clear unused files every so often.
That's true, however I think this is the only sensible solution. Naming conventions are hacky and finicky to manage over time, and trying to keep track of the form submission status over a short period of time is asking for trouble (what if they upload the image, leave their desk for an hour, come back, submit the form.. ie, what is a sensible timeout?).
A simple cron task to clear unused files would be reliable and safe, and very easy to implement compared to changing your upload and storage strategy.
精彩评论