0

I have a form like this:

<form method="POST">
  <input type="text" name="title">
  <textarea rows="4" cols="50" name="description"></textarea>
  <input type="file" multiple>
  <input type="submit">
</form>

I want to upload the files when user selects them (onChange event) and get an ID. With that ID I can create inputs like:

<input type="hidden" value="ID" name="images[]">

I can upload those file with Ajax, but let's suppose that for some reason user's browser closes or his computer shutdowns before user actually click submit button. How can I delete those files from server and database ?

PedroPombo
  • 23
  • 7
  • beforeunload event may do it! – Gacci May 05 '17 at 17:25
  • possible duplicate of [http://stackoverflow.com/questions/20853142/trying-to-detect-browser-close-event](http://stackoverflow.com/questions/20853142/trying-to-detect-browser-close-event) – Mahesh Singh Chouhan May 05 '17 at 17:36
  • beforeunload won't work when problems like forced shutdown occur – PedroPombo May 05 '17 at 17:38
  • Create a cron job that periodically checks for files uploaded but not associated in your database. – Nick May 05 '17 at 17:47
  • @Nick but I need to get file id from database. I thought about a "is_tmp" on "uploads" table – PedroPombo May 05 '17 at 18:00
  • Or better yet, upload the files to a temp folder and when the forms gets submited, move the file to their designated location. Then you can create a cron job that every night deletes the images from the temp folder. – Nick May 05 '17 at 18:02
  • If you need file id, then why not create a table in db, named temp_files insert when file upload with default status as 0 and image_id, turn status 1 when images[] array posted Run cron on server once a day and remove all images from folder those have status as 0 – Mahesh Singh Chouhan May 05 '17 at 20:05

0 Answers0