php上传时的唯一文件名

Umm*_*nta 7 php

我在我的网站上有一个提交图片上传的讨论表格.现在,我想在用户提交图像时上传唯一的文件名,以便我可以显示所有图像的结果,并且可以避免重复的文件名.怎么用PHP做到这一点?如果您需要我的表单流程代码,那么我将上传它.

非常感谢.

Joh*_*lia 10

您可以使用该uniqid()函数生成唯一ID

/**
 * Generate a unique ID
 * @link http://www.php.net/manual/en/function.uniqid.php
 * @param prefix string[optional] <p>
 * Can be useful, for instance, if you generate identifiers
 * simultaneously on several hosts that might happen to generate the
 * identifier at the same microsecond.
 * </p>
 * <p>
 * With an empty prefix, the returned string will
 * be 13 characters long. If more_entropy is
 * true, it will be 23 characters.
 * </p>
 * @param more_entropy bool[optional] <p>
 * If set to true, uniqid will add additional
 * entropy (using the combined linear congruential generator) at the end
 * of the return value, which should make the results more unique.
 * </p>
 * @return string the unique identifier, as a string.
 */
function uniqid ($prefix = null, $more_entropy = null) {}
Run Code Online (Sandbox Code Playgroud)


Bat*_*ins 8

您可以在日期中使用时间戳,这样您就不会获得两次相同的文件名/时间.

不确定你的代码到底是什么,但你可以做这样的事情:

$filename = uniqid() . $orig_filename;
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请阅读有关uniqid的PHP文档的此文档.它使用datetime输出唯一标识符字符串.