Saving or deleting uploaded image

I’m working on a multi-part form that will allow for images to be uploaded. The contents of the form are saved in a SESSION. So far, this is great, since the contents get destroyed if the user decided to abandon the form before completing it.

What if they upload an image and THEN abandon the form?

What’s best practice for handling that image if the user abandons the form?

Probably just scrap it along with everything else since if they change their mind they can simply reload it again. If your form is a long process, you might offer a “save draft” feature that will preserve what is already completed until they can return and complete the process, otherwise I would just scrap everything if the form is abandoned.

If you find the form is often abandoned unintentionally for some reason, you could save contents for a limited time allowing them to return and complete the process and then scrap it after the chosen time limit has expired.

Hope this helps…

How do I keep track of the image to scrap it?

If I upload it in a tmp directory and then run a cron job to empty the directory I most likely will delete something somebody is working on.

I suppose I could put the name of the image in a temp table and run a cron job on that…

I’m just wondering if there’s already a “best practice” solution for this so I don’t reinvent the wheel. :slight_smile:

I’m trying to remember how I did this…
When an image is first uploaded it gets a temp name, once the transaction is complete it gets a permanent name. The temp name gets recycled over and over again with subsequent images so there is no need to keep track or delete anything. The last uploaded image of a completed transaction will actually exist with its permanent name and its temp name until the next image is uploaded and takes over the temp name overwriting the previous image.

Here’s an example of a temp image and then the image with its permanent name:
TemporaryImage

PermanentImage

These are both the same picture right now, but once the particular contributor uploads another image the temporary image will change. Click on each link to see the naming scheme.

Yes, but after the image is renamed, what then?

  1. The user fills out the form
  2. The user uploads an image
  3. The image is renamed with its permanent name
  4. The user abandons the form
  5. The image is still sitting on the server

How do I delete that abandoned image if I don’t keep track of it?

In this situation it sounds like it would be safe to just delete files in the temporary dir if they are older than, for example 1 day.

how about;

  1. The user fills out the form

  2. The user uploads an image

  3. The image is renamed with the name of the session, in a temp folder #2

  4. The user abandons the form

  5. Delete everything in temp folder #2 nightly

  6. The user fills out the form

  7. The user uploads an image

  8. The image is renamed with the name of the session, in a temp folder #2

  9. The user continues with the form
    …and so on …

  10. The user fulfills the final step of you form and presses enter, you move the image from temp folder #2 to its permanent place with its permanent name

crmalibu and cups provided feasible possibilities.
For me personally I don’t care about the temporary image residing on the server. It sits there forever until the user uploads another that replaces the last one. I actually show the user what they last uploaded or started to upload.

Besides the options crmalibu and cups provided if it really matters to you make the upload of an image a timed process. 1). If the form is not completed in the allowable allotted time, delete the temporary image. 2). If the form is successfully completed delete the temporary image immediately after it gets its permanent name.


    if (FormCompleted){
              unlink('tmp.jpg');
    }elseif (TimeExpired){
              unlink('tmp.jpg');
    }

something like that should do it for you…

crmailbu, do you have an example of how I could do this in PHP. I could easily set up a cron job to do this? Would you suggest filemtime()?

cups, that’s an interesting take. crmalibu’s sounds easier, so long as I don’t delete an image that’s currently being worked on… :slight_smile:

You could use a php script, but, since you’re going to use a cron job, I would just have cron execute the following command. Much easier.

delete files that haven’t been modified in the last 1440 minutes


find /full/path/to/tmp/dir -type f -mmin +1440 -delete

But otherwise, yeah just write a php script that loops through files in a dir, testing their filemtime. Have cron execute that script.

Excellent! Thank you very much! :slight_smile: