Create Unique Image Names

前端 未结 15 2202
暗喜
暗喜 2021-01-06 08:02

What\'s a good way to create a unique name for an image that my user is uploading?

I don\'t want to have any duplicates so something like MD5($filename) isn\'t suita

15条回答
  •  一向
    一向 (楼主)
    2021-01-06 08:23

    If you actually need a filename (it's not entirely clear from your question) I would use tempnam(), which:

    Creates a file with a unique filename, with access permission set to 0600, in the specified directory.

    ...and let PHP do the heavy lifting of working out uniqueness. Note that as well as returning the filename, tempnam() actually creates the file; you can just overwrite it when you drop the image file there.

提交回复
热议问题