A
A
Alexey2016-02-08 07:37:07
PHP
Alexey, 2016-02-08 07:37:07

How not to overwrite pictures when saving?

Hello! It’s not the first week that I have been faced with a task, plz push me to the idea of ​​​​an editing algorithm.
On the culinary site, the recipe has 3 steps, each with one picture. Each picture is called the name of the recipe (in transliteration) plus the step number:

/image/kurica_s_gribami-1.jpg
/image/kurica_s_gribami-2.jpg
/image/kurica_s_gribami-3.jpg

In the database, their names are stored in a separate field in the record with their step.
When editing steps, the picture may change, it may remain the same, steps can be added or removed (added to any place or any step removed), and steps can be swapped.
Suppose I add another one after the first step, it turns out that I need to save a new picture under the name, /image/kurica_s_gribami-2.jpgrespectively, the one that was under this name will be called /image/kurica_s_gribami-3.jpg, etc.
What algorithm should be in order not to overwrite the old picture with a name /image/kurica_s_gribami-2.jpg, given that any step can be deleted in the same way, which means that it will be necessary to re-save in the opposite direction.
Even initially changing the names of those pictures that were already in the database and on the server does not work, because when
rename(/image/kurica_s_gribami-2.jpg, /image/kurica_s_gribami-3.jpg);
the third picture will be overwritten.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2016-02-08
@dzheka3d

Why is this a perversion? the step, as far as I understand, has its own id, so what prevents you from storing pictures with this id as a name?

Z
Zhainar, 2016-02-08
@zhainar

IMHO it will be more convenient to store pictures in a separate recipe folder
not
/image/kurica_s_gribami-n.jpg,
but
/image/kurica_s_gribami/n.jpg.
And it's probably better to store the order in a separate field images_order - just listing the order separated by commas, like "1.png,2.png,10.png,5.png".
And then adding and removing is just a matter of technique.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question