上传到服务器目录的标准?

发布于 2024-12-06 14:58:06 字数 468 浏览 1 评论 0原文

我有一个关于您正在使用的上传文件名标准(如果有)的主题/问题。想象一下,您有一个应用程序,允许将多种类型的文档上传到您的服务器并放入目录中。也许同一个文档甚至可以上传两次。通常,您在保存文档时必须进行某种唯一的文件名调整。假设它保存在目录中,而不是直接保存到数据库中。当然,元数据可能需要保存到数据库中。也许典型的 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 技术交流群。

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

发布评论

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

评论(1

挥剑断情 2024-12-13 14:58:06

我总是使用 md5()sha1() 对文件进行哈希处理,并将其用作文件名。

例如

3059e384f1edbacc3a66e35d8a4b88e5.ext

我会将原始文件名保存在数据库中,以防我需要它。

这将使文件名唯一,并确保您的服务器上不会多次出现相同的文件(因为它们具有相同的哈希值)。

编辑

如您所见,我与 zerkms 就我的解决方案进行了一些讨论,他提出了一些有效的观点。

我总是通过 PHP 提供文件,而不是让用户直接下载它们。

这有一些优点:

  1. 如果用户上传文件,我会将记录添加到数据库中。这将包含上传文件的用户、原始文件名和文件的哈希值。
  2. 如果用户想要删除一个文件,您只需删除该文件的用户记录即可。
  3. 如果删除后不再有用户拥有该文件,您可以删除该文件本身(或保留它)。
  4. 您不应该将文件保存在文档根目录中的某个位置,而应该保存在公众无法访问的其他位置,并使用 PHP 向用户提供文件。

正如 zerkms 所指出的,通过 PHP 提供文件的一个缺点是更消耗资源,尽管我发现这些优点值得额外的资源。

zerkms 指出的另一件事是,将文件保存为散列时并不真正需要扩展名(因为它已经在数据库中),但我总是喜欢通过简单地执行 <例如,代码>ls -la。然而,这也不一定。

I always just hash the file using md5() or sha1() 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:

  1. I would add records into the database if users upload a file. This would contain the user who uploaded the file, the original filename and tha hash of the file.
  2. If a user wants to delete a file you just delete the record of the user with that file.
  3. If no more users has the file after delete you can delete the file itself (or keep it anyway).
  4. You should not keep the files somewhere in the document root, but rather somewhere else where it isn't accessible by the public and serve the file using PHP to the user.

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.

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