2009-11-15 110 views
0

我正在开发一个php站点,我们必须从users.i上传图像,必须重命名该文件以防止图像名称发生冲突。生成唯一名称?

uniqid(rand(), true); 
and adding a large random number after it. 

这个工作是否完美。有什么建议么..??

它有关的图像生成唯一的名称.....

回答

8

功能tempnam()创建一个具有唯一名称的文件。

0

你可以使用Base36对SQL表中的AutoIncrement值(希望你使用SQL表)。

$filename = base_convert($last_insert_id, 10, 36); 
0

你必须依赖于“如何”大可以将你的图片库两种方式: 1.非大文件量我这样做

<?php 

$file = sanitize_file($file); // remove all no [az-09_] characters for safe url linking; 
$file_md5 = $md5($file); 
$file_extention = $md5($file); 

// since I assume the file should belongs to someone you can do this 
$file_name = $user_id . $file_md5 . $file_extension; 

// then save the file 

?> 
  • 选项.... CacheMogul。在这里你需要使用你的想象力。但对于大量的文件,这确实是一个很好的分片,所以你不需要担心文件夹的最大数量或大小
  • 1

    拿一个文件的MD5并使用它。 IIRC,碰撞的几率是64M中的1。如果这还不够,请在前面加上以秒或毫秒表示的时间戳。这样,即使生成了重复的md5,这些文件也必须在冲突的相同秒/毫秒内进入。