PHP 图像上传 - 通过 microtime() 实现唯一性
如果我将每个上传文件的文件名设为 PHP microtime(),那么冲突的风险是否现实?有没有办法进一步随机化文件名?
我预计大约一周内会有大约 20,000 次上传。即 2800 人/天或 119 人/小时(假设均匀分配)。有人有假设微时间是独一无二的经验吗?
谢谢!
If I make the filename of each uploaded file PHPs microtime(), are the risks of a collision realistic? And is there a way to further randomize the filenames?
I'm expecting about 20,000 uploads in about a week. That's 2800/day or 119/hour (assuming even distribution). Does anyone have any experience with assuming microtime to be unique?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的情况下生成随机文件名的最佳方法是使用 GUID。它们很棒,因为该算法即使对于多个服务器也能保证值的唯一性。如果您使用的是 Windows,则
com_create_guid
功能适合您。您还可以查看可用的
uniqid
函数适用于所有 PHP 平台。The best way to generate random file names in your case is to use GUIDs. They are great because this algorithm guarantees uniqness of the value even for multiple server. If you are using windows the
com_create_guid
function is for you.Also you can look at
uniqid
function which available for all PHP platforms.一种常见的方法是以YYYY/MM/DD/id.jpg 格式创建目录。这样您也不会遇到文件系统限制。
One common method is to create directories in the format of YYYY/MM/DD/id.jpg. That way you don't run into file system limitations as well.
我认为这不是一个大问题。 (有点偏执)
只需检查是否存在,如果存在,解决冲突(可能在 while 循环中,直到保证其唯一)。
我无法想象每微秒有这么多文件。
I think this is not a huuuuge problem. (kinda paranoid)
Just make a check if exists, if exists, solve the colision (maybe in a while loop until guarantee its unique).
I can't imagine so many file per micro-second.