0

In our web applications we struggle with a problem reoccurring constantly. An easy example is a blog post: it has a title, lead & body text and an image. Our UX designers would like to let users upload the image asynchronously. This becomes an issue when you want to create a new post. It isn't persisted yet, so there is no id (for example when you use the id for the path of the image) or you can't put the location of the image as column in the database.

We could create a "tmp" folder for blog posts to which a user uploads an image: but what would you do when the user never persists the post? Or when s/he comes back? What if another user uploads an image to create a new post too?

Is there any good practice around this problem? Our apps are written in php, but I think this doesn't really matter for the subject.

Jurian Sluiman
  • 13,498
  • 3
  • 67
  • 99

2 Answers2

1

Although we use S3 for file storage, the concept works the same;

  1. Create an entry in the app's database for the upload with a null reference. The reference to the entry gets sent back to the form and is posted along with it when the user is ready to submit.

  2. A cron runs periodically and removes any uploads/records where the reference is null after X number of hours.

Doing it this way means you can easily deploy your app across multiple front-ends without having to worry about where the file may be.

outeredge
  • 295
  • 1
  • 8
0

Write a script to clear out expired images that were never persisted and schedule it to run regularly using, for example, cron.

Quolonel Questions
  • 6,603
  • 2
  • 32
  • 33