上传到服务器目录的标准?
我有一个关于您正在使用的上传文件名标准(如果有)的主题/问题。想象一下,您有一个应用程序,允许将多种类型的文档上传到您的服务器并放入目录中。也许同一个文档甚至可以上传两次。通常,您在保存文档时必须进行某种唯一的文件名调整。假设它保存在目录中,而不是直接保存到数据库中。当然,元数据可能需要保存到数据库中。也许典型的 PHP 上传方法可能是该应用程序所使用的;很简单就能做到。
可能的文件命名标准:
1.) 附加具有唯一 ID 的文档文件名:image.png 更改为 image_20110924_ahd74vdjd3.png
2.) 也许使用 UUID/GUID 并将实际文件类型(元)存储在数据库中:2dea72e0-a341- 11e0-bdc3-721d3cd780fb
3.) 也许是组合: image_2dea72e0-a341-11e0-bdc3-721d3cd780fb.png
您能推荐一个好的标准方法吗? 谢谢,杰夫
I have a topic/question concerning your upload filename standards, if any, that you are using. Imagine you have an application that allows many types of documents to be uploaded to your server and placed into a directory. Perhaps the same document could even be uploaded twice. Usually, you have to make some kind of unique filename adjustment when saving the document. Assume it is saved in a directory, not saved directly into a database. Of course, the Meta Data would probably need to be saved into the database. Perhaps the typical PHP upload methods could be the application used; simple enough to do.
Possible Filenaming Standard:
1.) Append the document filename with a unique id: image.png changed to image_20110924_ahd74vdjd3.png
2.) Perhaps use a UUID/GUID and store the actual file type (meta) in a database: 2dea72e0-a341-11e0-bdc3-721d3cd780fb
3.) Perhaps a combination: image_2dea72e0-a341-11e0-bdc3-721d3cd780fb.png
Can you recommend a good standard approach?
Thanks, Jeff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我总是使用
md5()
或sha1()
对文件进行哈希处理,并将其用作文件名。例如
3059e384f1edbacc3a66e35d8a4b88e5.ext
我会将原始文件名保存在数据库中,以防我需要它。
这将使文件名唯一,并确保您的服务器上不会多次出现相同的文件(因为它们具有相同的哈希值)。
编辑
如您所见,我与 zerkms 就我的解决方案进行了一些讨论,他提出了一些有效的观点。
我总是通过 PHP 提供文件,而不是让用户直接下载它们。
这有一些优点:
正如 zerkms 所指出的,通过 PHP 提供文件的一个缺点是更消耗资源,尽管我发现这些优点值得额外的资源。
zerkms 指出的另一件事是,将文件保存为散列时并不真正需要扩展名(因为它已经在数据库中),但我总是喜欢通过简单地执行 <例如,代码>ls -la。然而,这也不一定。
I always just hash the file using
md5()
orsha1()
and use that as a filename.E.g.
3059e384f1edbacc3a66e35d8a4b88e5.ext
And I would save the original filename in the database may I ever need it.
This will make the filename unique AND it makes sure you don't have the same file multiple times on your server (since they would have the same hash).
EDIT
As you can see I had some discussion with zerkms about my solution and he raised some valid points.
I would always serve the file through PHP instead of letting user download them directly.
This has some advantages:
A disadvantage as zerkms has pointed out is that serving files through PHP is more resource consuming, although I find the advantages to be worth the extra resources.
Another thing zerkms has pointed out is that the extension isn't really needed when saving the file as hash (since it already is in the database), but I always like to know what kind of files are in the directory by simply doing a
ls -la
for example. However again it isn't really necessarily.