Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Uploading a picture while creating a new record


This is more of a conceptual question and not about the underlying programming technique.

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 enforced 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^e

like image 746
miCRoSCoPiC_eaRthLinG Avatar asked Jan 31 '11 05:01

miCRoSCoPiC_eaRthLinG


People also ask

Can we upload image in database?

The image can be uploaded directly to the database without storing on the server. But it will increase the database size and web page load time. So, it's always a good idea to upload images to the server and store file names in the database.


2 Answers

Kinda old question so you probably figured it out but this is how I go around it:

  1. 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.
  2. 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
  3. 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.
  4. 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)
  5. 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.

like image 99
Mark Avatar answered Sep 21 '22 02:09

Mark


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.

like image 44
jcomeau_ictx Avatar answered Sep 19 '22 02:09

jcomeau_ictx