Upload image with unique name

后端 未结 2 355
Happy的楠姐
Happy的楠姐 2021-01-16 03:55

So I am trying to upload some images to a folder on my server with the script below,but it saves every image as \"image.jpg\" and it overwrites the last uploaded image if I

2条回答
  •  时光取名叫无心
    2021-01-16 04:46

    A simple way to get a unique filename is to get the current Unix time in milliseconds and append (or prepend) that to the filename. The command to use is microtime().

    For example:

    $target_file = $target_dir . microtime() . basename($_FILES["fileToUpload"]["name"]);
    

    You could also try things like hashing the file to get a unique hash with low probability of collisions, but this is faster and just as effective.

提交回复
热议问题