Search code examples
phpmysqlfile-uploadfile-storage

How to make a name of file in such way 31f3207.jpeg


Possible Duplicate:
Unique key generation

How to automaticly make a new name of file when you upload it to server?

I upload a picture file with < input type="file" / >. File has name picture_1.jpg. But I'd like to store it in filesystem with name like this ec0b4c5173809b6aa534631f3207.jpg? How such new names are created? Is some special script/generator used to make such names?

For example picture in FB: http:// a3.sphotos.ak.fbcdn.net/hphotos-ak-snc6/190074_10150167009837952_8062627951_8311631_4439729_n.jpg.

The name of it is 190074_10150167009837952_8062627951_8311631_4439729_n.jpg. But original name was different for sure. So I'd like to change the name of uploaded file the same way. How is it possible?


Solution

  • In PHP I use uniqid() for naming files that I store from uploads

    If you want an extra long or seemingly more random id, you can sha1() or md5() the uniqid to create a hash.

    You could of course also use those two methods to create a hash of the filename.

    For example, the following code can be used to generate a new name for a file

    $file = 'somefile.jpg';
    $new_name = uniqid();
    $new_name .= '.'.pathinfo($file,PATHINFO_EXTENSION);