Hello,

I'm just trying to gauge how often this is used in real world development, it would seem that most would use databases for storing information instead of putting them in text files right?

I'm in my first PHP MySql college course. Last week we just finished a chapter on reading and writing to text files(.txt), making, copying, and deleting directories.

I'm just wondering how often in web development are you guys actually storing information in text files and then printing them back online?

Is this common? Or do you mostly use databases for storing information?(We havent covered databases yet, not for another week or two I think).

It depends on need. When I want to store some related informations I will store it in DB. for example, I have a business site so it must have businesses and users. So to store these related information and manipulate it efficiently database is best solution. Is n't it? But is I want to keep some configurations or something like that I will use file. Mostly using DB and in very few cases files.

Depends on the use case.

I always use a database for information that needs to be related together. I couldnt personally say 50/50 or 80/20 etc because my uses of flat files vs databases changes depending on the project i'm working on.

However, writing a text file is not just limited to plain text. You could use them for RSS feeds (XML) that get hit constantly, so you generate them on a regular basis to a physical file. Then the web server can handle many more requests for that file then if it were pulled dynamically.

Basic caching systems often take a page that is generated dynamically or parts of a page that are generated dynamically and write them to files for a period of time, to cut down on the number of database calls. This is a really basic usage of caching of course.

There are an endless amount of uses for flat text files, and knowing how to write to files and more importantly retrieve the pieces and parts you need from them.

From my usage of text files, although I often read text file (both txt and htm) I seem to never write to text files. I suppose MySQL has always been a better alternative especially since it is not garranteed that the data you write will be actually written to the text file. I learnt that when doing a loop that would append to a text file 10000 times and a few dozen would never be recorded.
So in my opinion, use databases if you can especially since they are password protected. Only use text files if you want computer generated html files or something like that.

My circumstance
a number of files, changed at midnight then remain the same for 24 hours.
first access after midnight script generates the files from database,
then they are served from the file system,
very basic cache system to reduce database calls.

Read files
a supplier changes their price lists, we receive .csv to parse and import to sql database, weekly(?)
there are 20K stock items in some supplier lists,
typing would take longer than the time between updates

I almost never write to txt, unless producing dynamic xml/xspf files, and even then the data is stored in db, filtered and then written. One other time is when I need static files (produced from my own cms) - but there may be better ways to do this. As mentioned, txt files can't be protected v. easily, although caching info is a good reason to use them (phpBB uses a mix of db info and cached files) - if the cached info isn't available, it makes a db call. Templating makes use of cached files (e.g. Smarty).

This article has been dead for over six months. Start a new discussion instead.