php 上传时的唯一文件名

发布于 2024-12-22 01:46:20 字数 123 浏览 5 评论 0原文

我的网站上有一个提交讨论表格,其中包含图像上传。现在,我想在用户提交图像时上传唯一的文件名,以便我可以显示所有图像的结果并避免重复的文件名。

我怎样才能用 php 做到这一点?如果您需要我的表单处理代码,我会上传它。

I've a submit discussion form with image upload in my website. Now, I want to upload a unique file name when the user submits their image so that I can show the result of all images and can avoid duplicate file names.

How can I do this with php? If you need my form process code then I'll upload it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

狠疯拽 2024-12-29 01:46:20

您可以使用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) {}

You can use the uniqid() function to generate a unique 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) {}
等数载,海棠开 2024-12-29 01:46:20

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

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

$filename = uniqid() . $orig_filename;

阅读有关 uniqid 的 PHP 文档更多信息。它使用日期时间输出唯一标识符字符串。

You could use a timestamp in the date, that way you won't get the same filename/time twice.

Not certain exactly what your code looks like but you could do something like this:

$filename = uniqid() . $orig_filename;

Read this documentation on the PHP docs about uniqid for more information. It uses the datetime to output a unique identifier string.

长伴 2024-12-29 01:46:20

如果需要唯一的名称,可以使用 uniqid

$filename = uniqid() . '.jpeg';

但请注意,它仅在一台机器上是唯一的,即如果您同时在两台不同的机器上运行它,它可以生成相同的东西。

If you need a unique name, you can use uniqid.

$filename = uniqid() . '.jpeg';

Be warned though that it will only be unique across one machine, ie it can generate the same thing if you run it at the very same time on two different machines.

长梦不多时 2024-12-29 01:46:20

IMO 的最佳选择是使用 sha1_file 它将创建一个独特的基于文件内容的哈希值。只有在相同图像或哈希冲突(概率为 2^160 中为 1)时才会发生冲突。您可以通过这种方式避免重复图像,但是因为您是从文件创建哈希,所以速度可能会较慢。

$filename = sha1_file($upload_file) . $ext;

另一个选项是tempnam它将创建一个具有唯一名称。

注意:如果 PHP 无法在指定的 dir 参数中创建文件,则会使用系统默认值。

基本示例:

$filename = tempnam($dir, $prefix);
unlink($filename)//we don't need the tempfile just the name

另请注意:如果更改文件名,它可能不是唯一的。

The best option IMO would be to to use sha1_file It will create a unique hash based on the file contents. This would only collide if its the same image or a hash collision which is a 1 in 2^160 chance. You can avoid having duplicate images this way however because you are creating a hash from the file it can be slower.

$filename = sha1_file($upload_file) . $ext;

Another option is tempnam It will create a temp file with a unique name.

Note: If PHP cannot create a file in the specified dir parameter, it falls back on the system default.

Basic example:

$filename = tempnam($dir, $prefix);
unlink($filename)//we don't need the tempfile just the name

Also note: If you change the filename it might not be unique.

你列表最软的妹 2024-12-29 01:46:20

我认为最简单的方法是将图像名称存储在数据库字段中,然后当用户上传图像时,通过对照现有文件名检查文件名来验证文件名是否唯一。如果您不希望用户必须等到表单提交后才收到错误或提示更改文件名,您可以使用 ajax / jquery 界面。

I think the simplest way would be to store the image name in a database field and then when the user is uploading their image to validate that the file name is unique by checking it against existing filenames. You can use an ajax / jquery interface if you don't want the user to have to wait til the form has been submitted to get an error or prompt to change their filename.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文