简体   繁体   English

存储网站内容:数据库还是文件?

[英]Storing website content: database or file?

I'm building a website, and I'm planning to publish various kinds of posts, like tutorials, articles, etc. I'm going to manage it with php, but when it comes to storing the content of each post, the very text that will be displayed, what's a better option: using a separate text file or adding it as an for each entry in the database? 我正在建立一个网站,我打算发布各种帖子,比如教程,文章等。我打算用php管理它,但是当涉及到存储每个帖子的内容时,非常将显示的文本,有什么更好的选择:使用单独的文本文件或将其添加为数据库中的每个条目?

I don't see a reason why not to use the database directly to store the content, but it's the first time I use a DB and that feels kind of wrong. 我没有看到为什么不直接使用数据库存储内容的原因,但这是我第一次使用数据库而且感觉有点不对劲。

What's your experience in this matter? 你在这件事上有什么经历?

Ok Friends I am visiting this question once again for the benefit of those who will read this answer. 好的朋友我再次访问这个问题是为了那些读过这个答案的人的利益。 After a lot of trial and error I have reached a conclusion that keeping text in database is a lot convenient and easy to manipulate. 经过大量的反复试验,我得出的结论是,在数据库中保存文本非常方便,易于操作。 Thus all my data is now with in database. 因此,我的所有数据现在都在数据库中。 Previously I had some details in database and the text part in file but now i have moved all to database. 以前我在数据库中有一些细节,文件部分在文件中,但现在我已将所有数据移到数据库中。

The only problem is that when editing your posts the field like title or tags or subject etc are changed on a simple html form. 唯一的问题是,在编辑帖子时,标题,标签或主题等字段会在简单的html表单上更改。 but for the main content I have created a text area. 但是对于主要内容我创建了一个文本区域。 however i just have to cut and copy it from the text area to my favorite text editor. 但是我只需要将它从文本区域剪切并复制到我最喜欢的文本编辑器中。 and after the editing copy and paste it back. 并在编辑后复制并粘贴回来。

some benefits that forced me to put every thing in database are 一些好处迫使我把所有东西放在数据库中

  1. EASY SEARCH: you can run quires like mysql LIKE on your text (specially main content). 轻松搜索:您可以在文本上运行像mysql LIKE这样的quires(特别是主要内容)。

  2. EASY ESCAPING: you can run commands easily on your data to escape special characters and make it suitable for display etc. EASY ESCAPING:您可以轻松地在数据上运行命令以逃避特殊字符并使其适合显示等。

  3. GETTING INPUT FROM USER: if you want the user to give you input it makes sense to save his input in database , escape it and manipulate it as and when required. 从用户获取输入:如果您希望用户为您提供输入,则将其输入保存在数据库中,将其转义并在需要时对其进行操作是有意义的。

  4. Functions like moving tables , back up, merging two records, arranging posts with similar content in sequential order... etc etc all is more easy in database than the file system. 移动表,备份,合并两个记录,按顺序排列具有相似内容的帖子等等功能在数据库中比文件系统更容易。

  5. in file system there is always the problem of missing files, different file names, wrong file shown for different title etc etc 在文件系统中总是存在丢失文件,不同文件名,不同标题显示错误文件等问题

  6. I do not escape user input before adding it to database just before display. 在显示之前将其添加到数据库之前,我不会转义用户输入。 this way no permanent changes are stored to the text.(i don't know if that's ok or not) 这样就没有永久性的更改存储到文本中。(我不知道是否可以)

Infact I am also doing something like you. 事实上我也在做像你这样的事情。 However I have reached the conclusion as explained below (almost the same as mentioned in the answer above me). 但是我已经得到了如下所述的结论(几乎与我上面的答案中提到的相同)。 I hope you must have made the decision by now but still I will explain it so that it is useful for future. 我希望你现在必须做出决定,但我仍然会解释它,以便它对未来有用。

My Solution: I have a table called content_table it contain details about each and every article, post or anything else that I write. 我的解决方案:我有一个名为content_table的表,它包含有关每篇文章,帖子或我写的任何其他内容的详细信息。 The main (text portion) of the articles/post is placed in a directory in a .php or .txt file. 文章/帖子的主要(文本部分)放在.php.txt文件的目录中。 When a user clicks on an article to read, a view of the article is created dynamically by using the information in database and then pulling the text part (I call it main content) from the .txt file. 当用户点击要阅读的文章时,通过使用数据库中的信息然后从.txt文件中拉出文本部分(我称之为主要内容)来动态创建文章视图。 The database contain information like _content_id_, creation date , author , catagory (most of this become meta tags). 该数据库包含类似_content_id_, 创建日期作者产品类别 (极本成为meta标签)的信息。

The two major benefits are: 两个主要好处是:

  1. performance since less load on datbase 性能因为数据库负载较少
  2. editing the text content is easy. 编辑文本内容很容易。

I am giving comments based on my experience , 我根据自己的经验给出评论,

Except attachments you can store things in DB, why because managing content,back up, restore ,querying , searching especially full text search will be easy. 除了附件,你可以将内容存储在数据库中,为什么因为管理内容,备份,恢复,查询,搜索特别是全文搜索会很容易。

Store attached files in some folder and keep path in DB tables. 将附加文件存储在某个文件夹中,并将路径保留在DB表中。

Even more if you r willing to implement search inside attachments you can go for some search engine like lucene which is efficient to search static contents. 如果你愿意在附件中实现搜索,你可以使用像lucene这样的搜索引擎来搜索静态内容。

keeping attachment in DB or in file system is upto the level of important to the files. 保持数据库或文件系统中的附件达到文件的重要级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM