文件上传和目录结构

发布于 2024-09-30 20:46:16 字数 451 浏览 0 评论 0原文

我想知道你们是否可以建议我构建文件上传的最佳方法。

我正在为一个网站制作一个表单,该表单包含一个图像上传部分。在本节中,图像在提交表单之前上传(通过 iframe)。由于表单尚未提交,我没有数据库记录来链接图像,因此我所做的就是在每次上传时将图像令牌插入到表单中。当表单提交时,我循环遍历令牌并将它们链接到新创建的数据库记录。

可能很重要:

图像可能已上传,但用户可能在提交之前丢弃它们,从而在数据库中留下未使用的记录,并在系统中留下未使用的图像。我必须经常清除未使用的文件。

我有点担心图像会以某种方式丢失其记录并在系统中“丢失”。不过我可能是偏执狂。

你们觉得怎么样?

编辑:我忘了提及,但我创建了图像的缩略图版本以与原始图像一起使用。我是否应该将“_thumb”附加到文件名并将其放在原始文件所在的位置?缩略图版本应该有自己的数据库记录吗?提前致谢。

I'd like to know if you guys could suggest me the best way to structure file uploads.

I'm making a form for a site and this form contains an image upload section. In this section, images are uploaded before the form is even submitted (via iframe). Since the form hasn't been submitted yet, I have no DB record to link the images to so what I do is on every upload I insert an image token to the form. When the form submits, I loop with through the tokens and link them to the newly created DB record.

Might be important:

Images might be uploaded but the user might discard them before submitting, thus leaving a unused record in the DB and a unused image in the system. I'd have to clear unused files every so often.

I'm somewhat afraid of images somehow losing their record and getting 'lost' in the system. I'm probably behind paranoid though.

What do you guys think?

EDIT: I forgot to mention but I create a thumbnail version of the image to go along with the original. Should I just append "_thumb" to the file name and put it wherever the original is? Should the thumbnail version have its own database record? Thanks in advance.

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

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

发布评论

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

评论(3

穿越时光隧道 2024-10-07 20:46:16

我做这个。

以免说您有一个“发布”部分。每次用户进入那里时,我都会创建一个令牌,该令牌在上传图像时作为隐藏字段发送,然后我将图像保存在临时文件夹中,其中的文件可以通过我的网络服务器的文档根访问。 就可以像这样命名图像,

<token>_<timestamp>.jpg

除此之外,我向每个文件添加一个时间戳,因此一旦图像上传,我

我回复图像名称并将其保存在隐藏值中。这样用户就可以查看/删除/等等图像,然后在提交表单后您就可以开始努力工作了。

要清理临时目录中的图像,我只需每隔几个小时迭代一次,并使用 glob 获取最旧的图像,然后删除它们。

I do this.

Lest say you have a "posting" section. Each time an user enters there I create a token, that token is sent as a hidden field when the image its uploaded, then I save the image on a temporal folder where files are accessible by the document root for my webserver. Along that I add to each file a timestamp, so an image could be named like this

<token>_<timestamp>.jpg

once the image its uploaded I reply with the image name and save it on a hidden value.

This way users can see/delete/etc the images and then you make the hard work once the form its submitted.

To clean images from the temp directory I just iterate it each several hours and use glob get the oldest and then delete them.

在将上传的文件从临时文件夹传输到最终目的地(无论是文件系统还是数据库中)之前,您不会向数据库提交任何内容。这样您就不会得到“孤立”记录。

编辑

您可以保存图像元数据,包括数据库中的所有图像和缩略图(作为 blob)。您还可以将图像保留在文件系统中,并仅将元数据保存在数据库中。如果您想保存每个图像的不同版本(维基风格),这完全取决于您。这允许用户回滚到之前的版本。您还可以简单地覆盖任何以前版本的映像,这可以节省大量磁盘空间。任何具有 _thumb 后缀的内容都可以自动与父图像关联。我不认为这是一个问题。

You don't commit anything to your database until you transferred the uploaded file from the temporary folder to the final destination, be it in the filesystem or a database. This way you don't end up with "orphan" records.

Edit:

You can save image metadata, including all images and thumbnails (as blobs) in the database. You can also keep images in the filesystem and save just the metadata in the database. It is really up to you if you want to save different versions of each image (wiki-style). This allows a user to rollback to a prior version. You can also simply overwrite any previous version of the image, which can save a lot of disk space. Anything that has a _thumb suffix can automatically be associated with a parent image. I don't see that as an issue.

非要怀念 2024-10-07 20:46:16

我必须经常清除未使用的文件。

确实如此,但我认为这是唯一明智的解决方案。随着时间的推移,命名约定的管理变得很棘手,并且试图在短时间内跟踪表单提交状态会带来麻烦(如果他们上传图像,离开办公桌一个小时,回来,提交,该怎么办?形式..即什么是合理的超时?)。

与更改上传和存储策略相比,一个简单的 cron 任务来清除未使用的文件将是可靠和安全的,并且非常容易实现。

I'd have to clear unused files every so often.

That's true, however I think this is the only sensible solution. Naming conventions are hacky and finicky to manage over time, and trying to keep track of the form submission status over a short period of time is asking for trouble (what if they upload the image, leave their desk for an hour, come back, submit the form.. ie, what is a sensible timeout?).

A simple cron task to clear unused files would be reliable and safe, and very easy to implement compared to changing your upload and storage strategy.

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