Create Unique Image Names

前端 未结 15 2221
暗喜
暗喜 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:32

    How big is the probablity of two users uploading image with same name on same microsecond ?

    try

        $currTime = microtime(true);
        $finalFileName = cleanTheInput($fileName)."_".$currTime;
    
    // you can also append a _.rand(0,1000) in the end to have more foolproof name collision
    
        function cleanTheInput($input)
        {
         // do some formatting here ...
        }
    

    This would also help you in tracking the upload time of the file for analysis. or may be sort the files,manage the files.

提交回复
热议问题