PHP: Uploading a picture while creating a new record
When I am in the process of creating a new record (say an Add Record screen), the record ID isn't available to me yet. The same screen has an option of uploading a picture too, using SWFUpload - which means the picture gets uploaded immediately.
Since the record id isn't available yet, there's no way to associate this particular picture with a record in the DB till I've hit the Save button.
Now suppose, after uploading the picture I navigate away from the Add record screen leaving an orphaned picture file on the server.
I can come back to the Add record screen and keep doing this on and on... and eat up a sizable chunk of the server space with unlinked / unused images.
How would you tackle this issue? What flow of logic should be 开发者_如何学JAVAenforced here?
One option (which I'm experimenting with right now), is to store the uploaded images in a tmp folder and moving them out and placing them in another proper folder once the record has been created (with a link the the picture in it's final resting place). And then, run nightly cron jobs to clear off the leftovers from the tmp folder.
Any other brighter and elegant approaches to this?
Thanks,
m^eKinda old question so you probably figured it out but this is how I go around it:
- Create a field in your db called ImgGuid with default value of 0 (varchar) to identify the image. I normally just use a Guid with or without image extension. Paths can be configured elsewhere.
- Create a hiddenfield in your upload page called hfImgGuid (or whatever) and load the ImgGuid value from the database, if there's no record the value should be 0
- Now in your upload handler check if the value is 0, if so create a new Guid for this image and return it to your swfupload and set your hfImgGuid with this value. Also store the Guid in the user Session.
- When the user presses 'Save' the hfImgGuid value will get saved in the 'ImgGuid' field and so your image is connected to a record. When the user want to change its photo later u can just choose to overwrite the current image (or create a new Guid and delete the old one but for this example its easier)
- Only problem we got is that when a user creates on 'New record' and he uploads a pic and then decides to leave the page you find yourself with an orphant image. So here comes the user session into place. When the user saves the page you can remove the image from the session, if the user doesnt save it and the session ends you can read the values and delete the orphant images.
I hope you get the idea, it can get very complex if you want to do it right and there are many ways to do it, but an ajax-style upload looks nice indeed and professional pages are hardly to be found without it.
you could eliminate at least part of the problem by storing paths to all uploaded items in a cookie (or cookies, if using different browsers) on the client computer.
alternatively, don't use SWFUpload, use plain-old HTML "input type=file" upload, and enforce record creation before addition of an associated photo. that would arguably be the cleanest solution to a potentially messy problem.
When file is uploaded it get temporary name by default, use it or overwrite with own one, this temp name should be used when record is saved to connect with image, when record inserted file name can be changed based on record ID, temporary name changed (remowed) after it...
精彩评论