I
I
Ivan Antonov2016-11-23 13:01:36
MySQL
Ivan Antonov, 2016-11-23 13:01:36

How to organize temporary storage of images?

Hello.
Users on the site posting ads in which you can insert pictures. The pictures are loaded and a separate record in the database is created for each. But what if the user changes his mind about placing an ad and simply closes the browser, the photo will remain and will lie on the server as unnecessary cargo.
Please tell me a reasonable way to prevent the appearance of unused files.
The organization of the database is as follows
. All files are stored in the files table .
And for ad images, there is another table offers_images with fields
offer_id - ad number in the offers table file_id
- file number in the files table

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene Wolf, 2016-11-23
@antonowano

Everything is extremely simple.
The first thing you need is to come to terms with the idea that a scheduler, such as CRON, is designed for such tasks. Not everyone likes it, but ... in fact, it is quite difficult to do without it. CRON is available on any normal hosting, not to mention some more complete options (VPS, Dedic., etc.)
You have pictures that are (should be) attached to some ad that is created earlier. With the help of CRON, you find ads that are no longer liquid (for example, underpublished) and, by a simple link, find their pictures and delete everything at once (both ads and pictures).
Another option: your ad is created later than the pictures. That is, the pictures exist for some time, until the complete formation of the ad, in a kind of vacuum. In this case, you add a field - "upload date" to each image and select all images whose upload date was more than a day ago and which are not tied to any ad (ie ad_id == NULL field ).
All this is done by CRON, which runs some controller method (judging by the fact that you are using Symfony). Usually this task is solved by the wget utility, which is launched by CRON. To prevent "accidental" launches by users, you can add a check for a GET parameter containing some hash.

A
Andrey, 2016-11-23
@andrushi4

I would add 2 more fields to the table:
- use flag in declaration (tinyint);
- upload date (timestamp).
By default, when loading an image, the value of the flag is zero. When the user posts an ad - one.
Further, once a day (at night, probably), the script searches the database for all records with the flag = 0 and the creation date of more than a day. Everything that finds - deletes (both records in a DB, and files).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question