从数据库中删除文件和相应的条目 [英] Deleting files and corresponding entries from database

查看:105
本文介绍了从数据库中删除文件和相应的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处理文件管理的网站。用户可以上传文件,添加描述,编辑和删除。这种情况下的最佳做法是什么?

I have a web site which handles files management. User can upload file, add description, edit and delete. What are the best practices for that kind of scenario?


  1. 我将文件存储在文件系统中。

  1. I store files in the file system.

我应如何处理文件的删除?在这种情况下,我必须删除实体:数据库中的文件和条目。第一种情况是删除文件,如果没有错误,则从数据库删除条目。但是,如果无法删除数据库中的条目,则无法恢复文件。因此,第二种情况相反:首先从数据库中输入,然后从文件中输入。但是同样,当无法删除文件时,我无法恢复db中的条目。哪种方法更好?还是还有其他?

How should I handle deleting of the file? In this case I have to entities to delete: file and entry in database. First scenario is that I delete file and if there was no error I delete the entry from database. But then if the entry from database couldn't be deleted I cannot restore my file. So the second scenario is oposite: first entry from the database and then file. But again, when file cannot be deleted I cannot restore the entry in db. Which approach is better? Or is there any other?

我认为这个问题对于所有Web编程语言和所有数据库引擎都是普遍的。但是,假设我拥有MySQL和PHP,因此无法从数据库存储过程级别删除文件。

I think the problem is universal for all web programming languages and all databases engines. But let's say that I have MySQL and PHP, so deleting the file from the level of database store procedure is not possible.

推荐答案

我通常发现最好进行软删除,尤其是对于数据库中的数据。

I usually find it best to go with doing soft deletes, especially for data in the database.

以这种方式进行操作,您可以简单地将文件放在新位置以表示它已被删除,并将数据库中的条目标记为已删除。然后,如果由于某种原因数据库删除失败,您仍可以使用该文件。

Doing it this way you could simply put the file in a new location to denote it as being deleted, and mark the entry in the database as being removed. This then allows you to still work with the file if the database delete fails for some reason.

将文件保存在新位置后,您可以备份该位置到另一个地方或进行设置以定期从该位置删除项目。

Once you have the file in a new location, you can either backup that location to another place or set something up to periodically delete items from that location.

这篇关于从数据库中删除文件和相应的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆