What is the best technique to save images in the mysql database.Should I save images as blob data or save it in directories.The images will be showed every time the user visits his or her profile. -Thanks in advance.
Asked
Active
Viewed 118 times
2
-
Please visit my [answer](http://stackoverflow.com/a/9141116/996493) – Lucifer May 19 '12 at 15:59
2 Answers
8
The best technique would be to save them in the file-system and save their paths in the database.
The database is meant for data, the file-system for files.
The technique I used in the past to make sure there are no duplicates was to hash the contents of the file and save it as the result, so I get something like:
42efb15825666918118ba72128195246dbae4976.jpg
The actual name is saved in the database. This was, the chance of having duplicates is negligible.

Madara's Ghost
- 172,118
- 50
- 264
- 308
-
if you upload the same file another time, you will have the same hash, no ? – Ahmed Laatabi May 20 '12 at 00:00
-
4
The best technique is what Truth said, in addition, to guarantee that your images will have a unique name, use the current timestamp to rename them in your directory.

Ahmed Laatabi
- 907
- 1
- 8
- 21
-
I used the user_id as their profile pictures name in the directory....thanks for the help – Tanbir Ahmed May 19 '12 at 16:13