PHP: Get file extension not working on uploads to S3

后端 未结 12 659
我寻月下人不归
我寻月下人不归 2020-12-06 17:49

I am using the Amazon S3 API to upload files and I am changing the name of the file each time I upload.

So for example:

Dog.png > 3Sf5f.png

Now I got

12条回答
  •  星月不相逢
    2020-12-06 18:29

    i am using this in my websites (and works fine for years):

    $file = 'yourOriginalfile.png';
    
    //get the file extension
    $fileExt = substr(strrchr($file, '.'), 1);
    
    //create a random name and add the original extension
    $fileUniqueName = md5(uniqid(mktime())) . '.' . $fileExt;
    
    rename($file,$fileUniqueName); 
    

    your function generates too short filenames (5 characters), this way creates longer filenames, avoiding to collide the file names.

    example output: aff5a25e84311485d4eedea7e5f24a4f.png

提交回复
热议问题