Jump to content

save files in db or in folder?


hassank1

Recommended Posts

Main reason is: it is less strain on database.

 

Reasons why you would need to store files in database are

1. You want operations on files to be logged into binary log

2. You want files to be part of database transactions (as in ACID transactions)

probably some others, but I can't imagine them right now

 

Link to comment
Share on other sites

There are pros and cons to both methods. The biggest benefit of storing in the DB (IMO) is that when you backup your database you effectively backup your media as well. It just makes it a lot cleaner/easier to do full backups and restores.

 

From the filesystem side of things you have performance. You don't have to do a lookup for the file and then create it on the fly, you simply immediately return the data making file operations quicker and easier.

 

If your dealing with files of less than 2mb the performance impact is minimal either route you go.

 

I've done sort of a hybrid approach which works fairly well. I store all the uploaded files in a directory that is not readable and create a hash of the filename... without an extension. This means that no one can access this directory, etc. Then I do a read into a variable and serve up the content by echoing out the appropriate mime types.

 

Using this approach I get most (but not all) of the performance related with the filesystem and some of the benefits of security/permission type of stuff via the database. Rather than dealing with file permissions I can create rules that I derive from a permissions system to determine of a user should be able to view the file or not.

 

But in short... the answer is - it depends. I'd suggest sharing some more specifics on the files you plan to store (including sizes), concerns about ease of backup, permission levels on these files, etc etc. Without that information at hand this is really just one of those debates that go back and forth.

Link to comment
Share on other sites

Some more cons of putting files in a database -

 

There is a maximum packet size that limits how much data can be put or gotten in a query that will limit the size of a file that can be placed into a database.

 

Backups of databases using .sql dumps, the most common way of backing up and transferring databases, outputs blob fields using two HEX characters per byte so the size of the blob data is double the actual file size stored in it. If you have 100MBytes of images, you get a 200MByte .sql dump file of that data.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.