在PHP中生成唯一的ID [英] Generating a unique ID in PHP

查看:216
本文介绍了在PHP中生成唯一的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在php中生成唯一ID,以便将用户上传的内容存储在FS上而不会发生冲突.我正在使用php,目前此小片段负责生成UID:

I'm trying to generate a unique ID in php in order to store user-uploaded content on a FS without conflicts. I'm using php, and at the moment this little snippet is responsible for generating the UID:

$id = tempnam (".", "");
unlink($id);
$id = substr($id, 2);

这段代码很丑陋:它在FS上创建一个临时文件并将其删除,仅保留生成的字符串的相关唯一部分.

This code is hideous: it creates a temporary file on the FS and deletes it, retaining only the relevant unique part of the generated string.

有没有更好的方法可以做到这一点,最好是没有任何外部依赖项?

Is there any better way to do this, most preferably without any external dependencies?

非常感谢!

推荐答案

string uniqid ([ string $prefix [, bool $more_entropy ]] )

根据当前时间(以微秒为单位)获取带前缀的唯一标识符.

Gets a prefixed unique identifier based on the current time in microseconds.

USAGE: $id = uniqid(rand(), true);

这篇关于在PHP中生成唯一的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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