由php生成的真正唯一的随机数? [英] truly unique random number generate by php?

查看:627
本文介绍了由php生成的真正唯一的随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个php脚本来托管用户上传的大量图像,为图像文件名生成随机数的最佳方法是什么,以便将来不会出现文件名冲突?就像Imageshack一样.谢谢.

I'm have build an up php script to host large number of images upload by user, what is the best way to generate random numbers to image filenames so that in future there would be no filename conflict? Be it like Imageshack. Thanks.

推荐答案

保留以前生成的所有先前编号的持久列表(在数据库表或文件中),并检查新生成的编号是否不在其中那些在名单上.如果您发现这样做的代价过高,请在足够数量的位上生成随机数,以确保发生冲突的可能性非常低.

Keep a persistent list of all the previous numbers you've generated(in a database table or in a file) and check that a newly generated number is not amongst the ones on the list. If you find this to be prohibitively expensive, generate random numbers on a sufficient number of bits to guarantee a very low probability of collision.

您还可以使用一种递增的方式来分配这些数字,例如基于当前时间的timestamp_part和random_part的串联,以确保在多个用户同时上传文件的情况下不会发生冲突.

You can also use an incremental approach of assigning these numbers, like a concatenation of a timestamp_part based on the current time and a random_part, just to make sure you don't get collisions if multiple users upload files at the same time.

这篇关于由php生成的真正唯一的随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆