Hello. Tell me who implements how to upload files to the server. This means not the download process itself, but the principle of storing the downloaded files. I wrote my script for uploading files and save them to the user's folder with the same file name as on the user's PC.

But I noticed that most of the sites after downloading rename the file name to its example, the same VK when downloading photos 001.jpg save it under the name QV00tYkq.jpg

Who knows, tell me what it is done for and how to create a short and unique name for the file? So far, only the thought of giving new names to files by uniqid () came to mind

  • one
    Again I ran into the problem of getting a short and unique name for the file. While stopped on such a solution base_convert (uniqid (), 16.36); // translation of the result of the function uniqid () from the 16th to the 36th numbering system. We get a unique string with a length of 10 characters. Example 'egbtn3cktx' I also use my own function, which translates the result of uniqid () operation into the 62nd system. As a result, we obtain the required length of the name of 8 characters consisting of numbers, large and small letters of the Latin alphabet. Example 'FGkg11Em' - Yur_OK

2 answers 2

what is it done for

Consideration of safety, so that the names could not intersect and that no one could go through all your pictures, having received a direct link.

how to create a short and unique file name

One of the ways to create a table with the names (meant the name after the download) of the downloaded files, where the link to the image file will be stored (maybe several links - big, medium, small). Further flight of fancy, for example: we take the id of the user who loaded the user + load time + id photos + salt, we drive them under the hash, and the name is ready

  • Regarding the rewriting of the controversial moment, how then to be in a situation when the user deliberately loads the image in order to overwrite the previous one? And how to get a short unique name with a length of 7-10 characters instead of 32, which returns md5 () - Yur_OK
  • one
    There is an algorithm that returns like 8 characters, I don’t remember exactly its name as well. look can here that you will find for yourself php.net/crypt - MDJHD

the best way to make the file name is its hash for example:

$name = md5(file_get_contents($file)); 
  • one
    after which it will be impossible to download 2 files with the same name? :) usually done: md5 (insertedImageId. 'some static salt') - Alex Kapustin
  • If you add a unique salt, try, for example, the time and date of loading, after which you already encrypt in md5 or the like. The chance that at the same time will download two files with the same name is minimal. - terantul
  • 2
    And where is the name, @shurik? file_get_contents () returns the contents of the file, not its name. Nevertheless, with this approach it will be impossible to download files with the same content, but with different names - MDJHD