So, once again, I was listening to the Pro::PHP Podcast (or is it Newscast? guys?). They were talking about putting files in to a database table. Now, most people will say you should never do this. And lots of time they are right. And once upon a time I agreed with them without question. Then I started living in the real world where sometimes you have to do things you never thought you would. Here are the two places where I stores files in a database.
Ever wrote software that has to be installed on n + infinity environments. It really sucks. Phorum version 3.3 or 3.4 was the first to allow attachments. We put them on disk. The instructions for setting up a spot on disk for attachments was longer than all the rest of the install document. The support questions were even worse. And there there were the people that left their old host and didn’t take the files with them. Just the database. DOH! So, in Phorum 5 we decided that for ease of use, there would be no need for Phorum to write to disk. Well, we tried really hard anyway. Its almost true. We only write cache data to disk. No config is written by the applicaiton and no permanent storage is written to disk. Phorum 5.2 (near beta) has a new file storage system that allows for modules to be written to store the data wherever you want it. There is already a disk storage module. But, already there are people asking really naive questions about it. Probably because someone told them to never store files in a table.
At dealnews we have a lot of images. Every deal gets its own image. We do 100+ deals a day. So, getting those images distributed to all the servers is a task. A deal could go from idea to live in 2 minutes. So, we decided to store the images in the database. 3 versions of the image in fact. With a little mod_rewrite magic we can pull the image from the DB and put it on disk on a front end server if its not on the server already. From that point forward the file is served off of disk. So, its a one time db hit per image per server. Not that big of a deal. We use a CDN now too, so it’s really not a big deal. We could probably skip the on disk part all together. Backups are easy. All the attributes of a deal are in the database. Not some in the db and some on some disk. We can just import the db to a test machine and have a fully functioning set of data to work with.