使用唯一文件名保存图像的最佳做法


Best practice for saving images with unique filename

在php服务器上保存上传图像的最佳方法是什么?假设我有一个带有图像的作品数据库。我应该添加一个随机字符串以防止覆盖其他图像吗?我应该用作品的唯一Id创建一个文件夹吗?最好的方法是什么?

完全由您决定。时间戳总是很有用的,因为除非你每秒上传一张以上的图像,否则它们一定是唯一的。

Chuck在任何唯一ID、用户ID、属性ID等中

例如<user_id>_<attribute_id>_2015021923559.jpg

我更喜欢文件名+日期时间

$removeExtension = explode('.',basename($_FILES["fileToUpload"]["name"]);
$target_file = $target_dir .$removeExtension[0].date("m-d-y").date("h-i-sa").".$removeExtension[1]"); //renamin the file to the current time as e.g. cover_02-03-15-01-13-18pm.jpg

我的方法是在文件名上添加上传时间:

$time = date('his');
$filename = $originalfilename."_".$time.".jpg";