简体   繁体   中英

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?

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. 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).

  2. EASY ESCAPING: you can run commands easily on your data to escape special characters and make it suitable for display etc.

  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. The main (text portion) of the articles/post is placed in a directory in a .php or .txt file. 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. The database contain information like _content_id_, creation date , author , catagory (most of this become meta tags).

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.

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.

keeping attachment in DB or in file system is upto the level of important to the files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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