views:

183

answers:

10

I have a form with action set to a php-file (verify.php). In the form, I have made a pretty nice picture upload section. After submitting the form, the verify.php is opened to verify the form filled in by the user.

The form values are all placed inside another form on the verify.php page. Only the image is uploaded to a folder on the server, and this is my problem. I want the image to be deleted if the user regrets (doesn't verify) putting in an ad, or if the user goes back to change the ad again.

How would I do this?

One way could be to delete the filename whenever page 'unLoads' (user hits back button). But that wont solve the problem if the user closes the browser on the verify.php page, because that doesnt count as an 'unload' does it?

Solutions?

If you need more input, tell me and I will update this post!

Thanks

A: 

I would put a time limit on how long a user has to confirm their ad posting. Then, have a monitoring job that will fire at that predetermined time and if the ad has not been confirmed, delete the image from the server.

Mike Clark
but what if the user goes back and choses another image, wont the process with the first time limit be aborted or something... hmmm
Camran
if the user has the option to upload a different file, then it seems like the upload process would check to see if the current process (user session, token, etc) already has an associated file on the server. if it does, delete that one, then save the new file to the server.
Mike Clark
A: 

I don't think it's very easy to detect when a user truly leaves a page. Most solutions I see usually perform some action (such as deleting temporary files) when the session times out.

FrustratedWithFormsDesigner
+1  A: 

Setup a database entry for each file with the timestamp. Then when your process is completed, remove that entry. Then, once a day, go through the table, delete all files still there, and remove those records from the table... it's the safest bet.

Palantir
you mean manually? probably the safest bet yes, but going through a folder every now and then isnt really what I want... but If there is no other way!
Camran
but no, you setup a cron script that does this cleanup, or you do it on each request (save a timestamp somewhere and check for it on each request: if it is too old, run the script, and update the timestamp). There is no such a thing as "manual" ;-)
Palantir
+1  A: 

Detecting "abandons" like browser closing is essentially impossible. There is no reliable way to track it on the server side in real-time. The easiest way to delete these images is to set up a running process that removes images associated with abandoned singups.

Dave Swersky
+1  A: 

normally, server side script don't know user leaves the page or close the browser, it can only check session timeout, but its normally 30 minutes or more depends on setting.

So if you really like to do it real-time, put ajax timer in the pages, and send hello request every 30 seconds or something like that to server, and then you could delete image file if there is no response more than 1 minute or something like that.

But sending ajax request to server every 30s or less could effect the server performance, if you have many users on it.

S.Mark
+7  A: 

Unfortunately this is a pretty tricky thing to do, the easier solution would be to check to see which files are old and delete them the next time someone accesses the page. This could be done completely through PHP and would be very reliable.

evolve
best one so far!
Camran
By the way is it possible to check file age somehow? or do I have to timestamp the filename for this?
Camran
You might want to take a look at http://php.net/manual/en/function.filemtime.php and http://www.php.net/manual/en/function.filectime.php.
evolve
A: 

Couldn't you just simply upload the image after verifying the submitted data?

Ben Fransen
A: 

Don't move the file to its permanent location until the user explicitly verifies. PHP puts uploaded files into a specific place, usually the /tmp directory (on *nix installations) which gets automatically cleaned out in fifo order as space is required. So if you don't move the file out of this special directory, then it will be autocleaned without your further intervention at an unspecified time in the future.

dnagirl
I have already moved the file out as I preview it and resize it!
Camran
But you don't have to. You can preview it from /tmp and you can explicitly save the resized version to /tmp and then move it when you get verification.
dnagirl
+1  A: 

I would name the image by epoch time (using the time() function) like 1258560055.jpg then build a cron job that goes in and deletes every image that's an hour, two hours or whatever after.

Jeremy Morgan
+1  A: 

Maybe I have misunderstood, but the process could be this:

1 User uploads file (that goes to *nix /tmp folder as normal)

2 You move the file to a second temp folder /upload_unconfirmed

3 When the user ACTUALLY does confirm the action, you then a) move it to your folder to keep it permanently, say, /clientfolder/theirusername.jpg b) then delete theirusername.jpg in /upload_unconfirmed

In the background run a cron to rm everyfile older than say 1 hour in /upload_unconfirmed/*

If each client only ever has one image, then subsequent uploads will overwrite their old file.

Cups
That's almost certainly how I would tackle it.
Al Everett