32

I have built a small web application in PHP where users must first log in. Once they have logged in, I intend on showing a small thumbnail as part of their "profile".

I will have to ensure the image is below a particular size to conserve space, or ensure it is a particular resolution, or both, or even perhaps use something like image magick to scale it down.
Not sure what the best approach for that is yet, any ideas welcome.

Also, I have been trying to work out if it is better to store the image in the users table of MySQL as a blob, or maybe a separate images table with a unique id, and just store the appropriate image id in the users table, or simply save the uploaded file on the server (via an upload page as well) and save the file as theUsersUniqueUsername.jpg. Best option?

I found a tutorial on saving images to mysql here: http://www.phpriot.com/articles/images-in-mysql

I am only a hobby programmer, and haven't ever done anything like this before, so examples, and/or a lot of detail is greatly appreciated.

palacsint
  • 28,416
  • 10
  • 82
  • 109
occhiso
  • 3,161
  • 4
  • 22
  • 16
  • 3
    Best practice is definitely to store the images as a file and a reference in the database, as identified below. – Toby Hede Feb 09 '09 at 11:44
  • 1
    Thanks to everyone who answered, I have used the gd to library to scale the uploaded image down to thumbnail size, and stored it on the filesystem with the username as the filename. Works great. – occhiso Feb 13 '09 at 23:55

16 Answers16

41

Always depends of context, but usually, I store a user image on the filesystem in a folder called /content/user/{user_id}.jpg and try to bother the database as little as possible.

palacsint
  • 28,416
  • 10
  • 82
  • 109
Mario
  • 1,515
  • 10
  • 10
  • 15
    There are cases when storing images in the database make sense. E.g. if you have multiple instances of your application server. If you would store images on the file system, you would either need to sync the images across all instances of the app server, or implement some image server solution. Another advantage of having the images in the db is that all of your data is in one place (easier to backup and replicate if needed). – Artem Goutsoul Jan 31 '12 at 09:41
  • 2
    Storing images on filesystem usually requires add write permission in the destination folder of the images to the account that run the application server. This shouldn't be a problem but be careful at this point. – user1011138 Feb 05 '13 at 12:46
23

I would recommend storing the image as a file and then have the file URI in the database. If you store all the images in the database, you might have some problems with scaling at a later date.

Check out this answer too:

Microsoft's advice for SQL Server used to be, for speed and size, store images in the file system, with links in the database. I think they've softened their preference a bit, but I still consider it a better idea certainly for size, since it will take up no space in the database.

Community
  • 1
  • 1
Philip Morton
  • 129,733
  • 38
  • 88
  • 97
  • Although this doesn't come without its problems, especially when the files change their directory. – Kieran Senior Feb 09 '09 at 11:45
  • But why would they do that? Even if they did move physically, the web server could be configured to preserve their URI anyhow. This also allows you to easily put the images on a server all by themselves if needed, which doesn't work if they're in the same db as anything else. – Dave Sherohman Feb 09 '09 at 13:40
  • 1
    How is an advice for SQL Server applicable to MySQL, an entirely different product? – NullUserException Nov 07 '11 at 23:55
18

The overhead using BLOB is a lot less than most people would have you believe, especially if you set it up right. If you use a separate server just running the DB to store binary files then you can in fact use no file-system at all and avoid any overhead from the file-system

That said the easiest/best way unless you have a couple of servers to yourself is storing them in the filesystem

  • Do not store the absolute URL of the file in your DB, just the unique part (and possibly a folder or two), e.g. 2009/uniqueImageName.jpg or just uniqueImageName.jpg. Then in your pages just add the host and other folders onto the front, that way you have some flexibility in moving your images - all you'll need to change is a line or two in your PHP/ASP.NET page.

  • There is no need to store outside the document root for security - a .htaccess file with DENY FROM ALL will work the same and provide more flexibility

  • No need to 'shunt' images so much for security, just have a getImage.php page or something, and then instead of inserting the actual URL in the src of the image, use something like getImage.php?file=uniqueImageName.jpg. Then the getImage.php file can check if the user is authorised and grab the image (or not).

  • Use a name which is guaranteed to be unique (preferably an integer i.e. primary key) when storing, some file-system (i.e. Windows) are case-insensitive, so JoeBloggs.jpg and joebloggs.jpg are unique for the database, but not for the file-system so one will overwrite another.

  • Use a separate table for the images, and store the primary key of the image in the users table. If you ever want to add more fields or make changes in future it will be easier - it's also good practice.

If you are worried about SEO and things like that, store the image's original file name in another field when you are uploading, you can then use this in your output (such as in the alt tag).

palacsint
  • 28,416
  • 10
  • 82
  • 109
Joseph Earl
  • 215
  • 2
  • 2
  • Having an entirely separate server isn't very practical on most cases. Going with database linked system is usually the way to go. – Madara's Ghost Sep 14 '12 at 16:36
  • 3
    There's nothing inherently wrong with storing images in a database. The points outlined here are valid and should be followed. However, if you expect to serve up every image via something like `image.php?file=1234` you're going to run into serious performance problems. It's better to dump the images to a CDN or flat-file to be served by apache and use the DB as a persistence layer/fallback. – Mike B Sep 14 '12 at 16:51
15

Challenging the Conventional Wisdom!

Of course it is context dependent, but I have a very large application with thousands of images and documents stored as BLOBS in a MySQL database (average size=2MB) and the application runs fine on a server with 256MB of memory. The secret is correct database structure. Always keep two separate tables, one of which stores the basic information about the file, and the other table should just contain the blob plus a primary key for accessing it. All basic queries will be run against the details table, and the other table is only access when the file is actually needed, and it is accessed using an indexed key so performance is extremely good.

The advantages of storing files in the database are multiple:

  1. Much easier backup systems are required, as you do not need to back up the file system
  2. Controlling file security is much easier as you can validate before releasing the binary (yes, you can store the file in a non-public directory and have a script read and regurgitate the file, but performance will not be noticeably faster.
  3. (Similar to #1) It cleanly separates "user content" and "system content", making migrations and cloning easier.
  4. Easier to manage files, track/store version changes, etc, as you need fewer script modifications to add version controls in.

If performance is a big issue and security and backups aren't (or if you have a good fs backup system) then you can store it the the FS, but even then I often store files (in the case of images) in the DB and building a caching script that writes the image to a cache folder after the first time it's used (yes, this uses more HD space, but that is almost never a limiting factor).

Anyway, obviously FS works well in many instances, but I personally find DB management much easier and more flexible, and if written well the performance penalties are extremely small.

Ben D
  • 14,321
  • 3
  • 45
  • 59
10

We created a shop that stored images in the DB. It worked great during development but once we tested it on the production servers the page load time was far too high, and it added unneccessary load to the DB servers.

While it seems attractive to store binary files in the DB, fetching and manipulating them adds extra complexity that can be avoided by just keeping files on the file system and storing paths / metadata in the DB.

This is one of those eternal debates, with excellent arguments on both sides, but for my money I would keep images away from the DB.

Neil Aitken
  • 7,856
  • 3
  • 41
  • 40
9

I recently saw this tip's list: http://www.ajaxline.com/32-tips-to-speed-up-your-mysql-queries

Tip 17: For your web application, images and other binary assets should normally be stored as files. That is, store only a reference to the file rather than the file itself in the database.

So just save the file path to the image :)

AntonioCS
  • 8,335
  • 18
  • 63
  • 92
6

I have implemented both solutions (file system and database-persisted images) in previous projects. In my opinion, you should store images in your database. Here's why:

  1. File system storage is more complicated when your app servers are clustered. You have to have shared storage. Even if your current environment is not clustered, this makes it more difficult to scale up when you need to.
  2. You should be using a CDN for your static content anyways, and set your app up as the origin. This means that your app will only be hit once for a given image, then it will be cached on the CDN. CloudFront is dirt cheap and simple to set up...there's no reason not to use it. Save your bandwidth for your dynamic content.
  3. It's much quicker (and thus cheaper) to develop database persisted images
  4. You get referential integrity with database persisted images. If you're storing images on the file system, you will inevitably have orphan files with no matching database records, or you'll have database records with broken file links. This WILL happen...it's just a matter of time. You'll have to write something to clean these up.

Anyways, my two cents.

Dustin
  • 574
  • 6
  • 13
  • Interesting point about the CDN (+1 for that). If the app has a catching layer in front, the database overhead is not important any more. – Daniel Nov 28 '14 at 05:02
4
  1. What's the blob datatype for anyway, if not for storing files?
  2. If your application involves authorisation prior to accessing the files, the changes are that you're a) storing the files outside of DOCUMENT_ROOT (so they can't be accessed directly; and possibly b) sending the entire contents of the files through the application (of course, maybe you're doing some sort of temporarilly-move-to-hashed-but-publicly-accessible-filename thing). So the memory overhead is there anyway, and you might as well be retrieving the data from the database.
  3. If you must store files in a filesystem, do as Andreas suggested above, and name them using something you already know (i.e. the primary key of the relevant table).
Sam Wilson
  • 4,402
  • 4
  • 29
  • 30
2

I think that most database engines are so advanced already that storing BLOB's of data does not produce any disadvantages (bloated db etc). One advantage is that you don't have any broken links when the image is in the database already. That being said, I have myself always done so that I store the file on disk and give the URI to the database. It depends on the usage. It may be easier to handle img-in-db if the page is very dynamic and changes often - no namespace -problems. I have to say that it ends down to what you prefer.

Jens Jansson
  • 4,626
  • 4
  • 25
  • 29
2

I would suggest you do not store the image in your db. Instead since every user will be having a unique id associated with his/her profile in the db, use that id to store the image physically on the server.

e.g. if a user has id 23, you can store an image in www.yourname.com/users/profile_images/23.jpg. Then to display, you can check if the image exists, and display it accordingly else display your generic icon.

Alec Smart
  • 94,115
  • 39
  • 120
  • 184
2

As the others suggested:

  • Store the images in the filesystem
  • Do not bother to store the filename, just use the user id (or anything else that "you already know")
  • Put static data on a different server (even if you just use "static.yourdomain.com" as an alias to your normal server)

Why ?

The bigger your database gets the slower it will get. Storing your image in the database will increase your database size. Storing the filename will increase your database size.

Putting static data on a different server (alias):

  • Makes scaling up a lot easier
  • Most browsers will not send more than two requests to the same server, by putting static data on a "second" server you speed up the loading
Andreas
  • 1,379
  • 10
  • 12
  • With HTTP/2.0 getting more adoption these things (static... domain) change slightly. Just remember that the answer is from 2009 ;) – Andreas Mar 12 '15 at 14:16
1

After researching for days, I made a system storing images and binaries on the database.

It was just great. I have now 100% control over the files, like access control, image sizing (I don't scale the images dynamically, of course), statistics, backup and maintenance.

In my speed tests, the sistem is now 10x slower. However, it's still not in production and I will implement system cache and other optimizations.

Check this real example, still in development, on a SHARED host, using a MVC: http://www.gt8.com.br/salaodocalcado/calcados/meia-pata/

In this example, if a user is logged, he can see different images. All products images and others binaries are in DB, not cached, not in FS.

I have made some tests in a dedicated server and results were so far beyond the expectations.

So, in my personal opinion, although it needs a major effort to achieve it, storing images in DB is worth and the benefits are worth much more the cons.

0

These are the pros of both solutions

In BLOBS :

1) pros : the easiness to mange clusters since you do not have to handle tricky points like file syncs between servers

2) DB backups will be exhaustive also

In files

1) Native caching handly (and that's the missing point of previous comments, with refresh and headers that you won't have to redesign in DB (DB are not handling last modification time by default)

2) Easiness of resizing later on

3) Easiness of moderation (just go through your folders to check if everything is correct)

For all these reasons and since the two pros of databases are easier to replicate on file system I strongly recommend files !

Irem
  • 1
0

In my case, i store files in file system. In my images folder i create new folder for each item named based on item id (row from db). And name images in an order starting from 0. So if i have a table named Items like this:

       Items      
|-----|------|-----|
| ID  | Name | Date|
|-----|------|-----|
| 29  | Test1| 2014|
|-----|------|-----|
| 30  | Test2| 2015|
|-----|------|-----|
| 31  | Test3| 2016|
|-----|------|-----|

my images directory looks like something like:

images/
      29/
      30/
      31/


images/29/0.png
images/29/1.jpeg
images/29/2.gif

etc.

Elnoor
  • 3,401
  • 4
  • 24
  • 39
0

As everybody else told you, never store images in a database. A filesystem is used to store files -> images are files -> store them in filesystem :-)

Lucacri
  • 150
  • 4
  • 4
0

Just tested my img's as blob, so. This solution working slower than images on server as file. Loading time should be same with images from DB or http but is't. Why? Im sure, when images are files on server, browser can caching it and loading only once, first time. When image going form DB, every time is loaded again. That's my oppinion. Maybe Im wrong about browser caching, but working slower (blob). Sry my English, whatever ;P

Dudeist
  • 363
  • 1
  • 4
  • 13
  • 1
    I know little about browser caching but I doubt that this is correct because there's no way for the browser to know where the image data comes from really. – Jonathan May 16 '12 at 21:25