9

Hi is there a way to rename uploaded file using VichUploaderBundle? If I upload two image with same name it will remove the old one and it's a problem for me.

I have configuration exactly the same like in the doc

smyslov
  • 1,279
  • 1
  • 8
  • 29
H.W.
  • 343
  • 1
  • 5
  • 21
  • 3
    You'll probably need to implement a [custom file namer](https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/file_namer/howto/create_a_custom_file_namer.md). – Yoshi Aug 04 '15 at 10:01
  • 4
    check the `File Namer` component [here](https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/namers.md). I usually use the standard namer_uniqid as described in the doc – Matteo Aug 04 '15 at 10:02

2 Answers2

14

For this kind of issues, the bundle provide "file namers". They are simple services used to name files, in particular to avoid collisions on the filesystem.

You probably want to use the vich_uploader.namer_origname file namer as it will rename your uploaded files using a uniqueid as the prefix of the filename and keeping the original name and extension (a file named foo.jpg will be stored as 50eb3db039715_foo.jpg)

Jaime Roman
  • 749
  • 1
  • 11
  • 26
K-Phoen
  • 1,260
  • 9
  • 18
  • thank you very much for your answer, please check the url of the link, I think it has changed to:https://github.com/dustin10/VichUploaderBundle/blob/master/docs/namers.md – Jaime Roman Apr 10 '21 at 05:05
2

You could use the namer in your vich_uploader.yaml

vich_uploader:
    db_driver: orm

    mappings:
        product_image:
                  namer: vich_uploader.namer_origname
L3xpert
  • 1,109
  • 1
  • 10
  • 19