473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Where to store pictures, in DB or in files?

I'm making some kind of news-board site on PHP (tthat's why I guess
it's the right place to post the question here). Each entry would
have a small picture (less then 100kb). Taking into consideration all
pros and cons the question is where to store these pictures in DB or in
files?

Jul 7 '06 #1
8 1645

ro********@gmail.com wrote:
I'm making some kind of news-board site on PHP (tthat's why I guess
it's the right place to post the question here). Each entry would
have a small picture (less then 100kb). Taking into consideration all
pros and cons the question is where to store these pictures in DB or in
files?
Files. You can link to them in an <imgtag easier, don't have to
write code to extract the image and send the appropriate headers, and
can easily view/modify/ them using ftp/samba/etc. Store a path to the
file in the database instead.

Jul 7 '06 #2
"Richard Levasseur" <ri********@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
>
ro********@gmail.com wrote:
>I'm making some kind of news-board site on PHP (tthat's why I guess
it's the right place to post the question here). Each entry would
have a small picture (less then 100kb). Taking into consideration all
pros and cons the question is where to store these pictures in DB or in
files?

Files. You can link to them in an <imgtag easier, don't have to
write code to extract the image and send the appropriate headers, and
can easily view/modify/ them using ftp/samba/etc. Store a path to the
file in the database instead.
Depending on how many files you plan to accumulate, it is traditional to use
a directory structure where you keep the file in a directory whose path
components are the file index moduli of different prime numbers. For
example, choose 2, 3, and 5 as the prime numbers, then

File #1 goes in 1/1/1/1/filename.
File #2 goes in 0/2/2/2/filename.
File #3 goes in 1/0/3/3/filename.
File #4 goes in 0/1/4/4/filename.
File #5 goes in 1/2/0/5/filename.
File #n goes in (n mod 2)/(n mod 3)/(n mod 5/n/filename.

As long as you choose different prime numbers, you won't get the same hashed
directory until a period which is the product of the primes.

The moduli are cheap to compute for a computer.

Files are the superior way. In addition to all other advantages, if
something goes monstrously wrong on the server or in the database product,
the files can be recovered separate from the database.

Dave.

Jul 7 '06 #3

ro********@gmail.com wrote:
I'm making some kind of news-board site on PHP (tthat's why I guess
it's the right place to post the question here). Each entry would
have a small picture (less then 100kb). Taking into consideration all
pros and cons the question is where to store these pictures in DB or in
files?
Keeping images in the database makes them easier to manage. No worry
about duplicate filename. If your database supports cascade delete then
the images are purged automatically when the article is removed. And a
foreign key constraint could guarantee that the images won't go
missing. Keeping them in the database also makes backup easier.

The downside is performance and resource usage. The web server is
extremely efficient at serving static files. Loading an image from the
database is relatively slow, on the other hand, and uses a good chunk
of memory.

Jul 7 '06 #4
David T. Ashley wrote:
"Richard Levasseur" <ri********@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...

ro********@gmail.com wrote:
I'm making some kind of news-board site on PHP (tthat's why I guess
it's the right place to post the question here). Each entry would
have a small picture (less then 100kb). Taking into consideration all
pros and cons the question is where to store these pictures in DB or in
files?
Files. You can link to them in an <imgtag easier, don't have to
write code to extract the image and send the appropriate headers, and
can easily view/modify/ them using ftp/samba/etc. Store a path to the
file in the database instead.

Depending on how many files you plan to accumulate, it is traditional to use
a directory structure where you keep the file in a directory whose path
components are the file index moduli of different prime numbers. For
example, choose 2, 3, and 5 as the prime numbers, then

File #1 goes in 1/1/1/1/filename.
File #2 goes in 0/2/2/2/filename.
File #3 goes in 1/0/3/3/filename.
File #4 goes in 0/1/4/4/filename.
File #5 goes in 1/2/0/5/filename.
File #n goes in (n mod 2)/(n mod 3)/(n mod 5/n/filename.

As long as you choose different prime numbers, you won't get the same hashed
directory until a period which is the product of the primes.

The moduli are cheap to compute for a computer.

Files are the superior way. In addition to all other advantages, if
something goes monstrously wrong on the server or in the database product,
the files can be recovered separate from the database.

Dave.
Another option would be to name the file after the primary key, or
other unique index, of the table.

Jul 8 '06 #5
"Richard Levasseur" <ri********@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
Another option would be to name the file after the primary key, or
other unique index, of the table.
The reason for the prime moduli directory scheme is that Unix directory
operations become inefficient beyond a few hundred entries in a directory.

The prime moduli scheme is designed to avoid this by filling up the
lowest-level directories evenly.

It isn't clear that naming the file after the primary key would have the
same advantage.

The example I gave was with three primes. In practice, one should use
primes not larger than 200 and enough of them so that their product exceeds
2^32.


Jul 8 '06 #6
TW

David T. Ashley wrote:
"Richard Levasseur" <ri********@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
Another option would be to name the file after the primary key, or
other unique index, of the table.

The reason for the prime moduli directory scheme is that Unix directory
operations become inefficient beyond a few hundred entries in a directory.

The prime moduli scheme is designed to avoid this by filling up the
lowest-level directories evenly.

It isn't clear that naming the file after the primary key would have the
same advantage.

The example I gave was with three primes. In practice, one should use
primes not larger than 200 and enough of them so that their product exceeds
2^32.
That is justa maintenance nightmare for the average person...Someone's
poor bastard's gonna go implement this technique to only realize that
s/he shot himself in the foot because they'll have to figure out what
folder to place an image into. What about when it's time to delete a
file? Now they'll have to script it out (if feasible). That's just too
much work for something that should simply be an intelligible file
structure.

Use that method if you find that you must, but 200 entries? That's on
the low side. I know a site right now with major traffic and about 4000
images in a single folder...it's hardly slow to serve them up.

Perhaps read operations don't suffer as much (or at all) as writes.
I've seen qmail fill a folder with 100,000+ files. Read access times
were just fine.

I'm all for efficient design, but practicality rules.

Jul 8 '06 #7
In message <jQ*******************@fe67.usenetserver.com>, David T.
Ashley <dt*@e3ft.comwrites
>"Richard Levasseur" <ri********@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegr oups.com...
>Another option would be to name the file after the primary key, or
other unique index, of the table.

The reason for the prime moduli directory scheme is that Unix directory
operations become inefficient beyond a few hundred entries in a directory.
Indeed, AFAIK it starts at the beginning of the directory and searches
sequentially until it finds a hit.. There is no structure of any kind,
unlike the old System 10 / System 25 computers, where the directories
were tree structures and so much more efficient for large numbers of
files.

However there are schemes that people find easier to understand... For
example, Demon's home directory for a user 'picture' will be similar to
/home/p/i/c/picture. What makes it a good scheme IMHO is that the path
can be derived from the user's name..

At present I don't have enough images anywhere for this to be a problem
but I would adopt a similar approach:

$IMGPATH/0 holds images 0 to 99
$IMGPATH/1 holds images 100 to 199

and so on.

So, the database doesn't even need to hold the full path to the file -
the code can easily derive it from $IMGPATH and the file name. Moving
$IMGPATH doesn't involve altering the database contents, and (obviously
I hope) the code to derive the path would be a function or class so that
if you ever re-organise the structure there is only one place to have to
change the code.
--
Surfer!
Email to: ramwater at uk2 dot net
Jul 8 '06 #8
TW wrote:
Perhaps read operations don't suffer as much (or at all) as writes.
I've seen qmail fill a folder with 100,000+ files. Read access times
were just fine.
That folder might be mounted on something more advanced like ReiserFS.

In any event, it's worth remember that at the end of the day, the image
file has to be sent across the Internet. Network latency will eat up
what little you gain.

Jul 8 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bob Bedford | last post by:
I've been able to parse the XML files used to update my database. Now, I receive image in XML files, like this: <PICTURES COUNT="3" CDATA="1"> <PIC NR="1"><!]></PIC> <PIC...
1
by: Sven Steinacker | last post by:
Hi, XSLT files generated by Microsoft InfoPath applied to an applicable XML file produces quite decent HTML presentations. At least with Mozilla and, of course, IE 6.0 the results look the same...
5
by: Guadala Harry | last post by:
What are my options for *securely* storing/retrieving the ID and password used by an ASP.NET application for accessing a SQL Server (using SQL Server authentication)? Please note that this ID and...
7
by: Alan Silver | last post by:
Hello, I am just looking at VWD and seeing what needs doing to take an existing site I've written by hand and importing it into VWD. I've already discovered that I need to rename my code-behind...
2
by: Nicolas | last post by:
How to store multiple files in only one sql blod field in sql database Like I got a record for a house: ID Name addresss price attachments The field "attachments" may contains documents...
21
by: MLH | last post by:
If I choose a command button on a form in design view, open the properties box and click the Picture property and its wiz button, I'm presented with a lengthy list of pictures. Everything from Add...
13
by: Viken Karaguesian | last post by:
Hello everyone, Can anyone recommend a good online site to learn PHP? The W3Schools website is quite lacking - leaves much to be desired. I'm sure there are many places, but which ones are good?...
3
by: JM | last post by:
Before storing information from a form in database I perform follwing operations on it : $path = mysql_real_escape_string(strip_tags(trim(urldecode($_POST)))); $summary =...
5
by: Alexnb | last post by:
Hello I am sure most of you are familiar with py2exe. I am having a bit of a problem. See the program has a few pictures involved and the .ico it uses for the windows. However, the pictures are...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.