Problem Solution Discussion Storing Images or Other Binary Data

chapter number was invalid, but handle that case sensibly. sub get_verses { my dbh, cnum = _; my ref = dbh-selectall_arrayref SELECT vnum, vtext FROM kjv WHERE bname = Esther AND cnum = ?, undef, cnum; my verses = ; foreach my row_ref {ref} { verses .= p escapeHTML row_ref-[0]. row_ref-[1]; } return verses eq no verses? ? p No verses in chapter cnum were found. : p Chapter cnum: . verses; }

17.6.6 See Also

est her2.pl exam ines it s execut ion environm ent using t he param funct ion. Web script param et er processing is discussed furt her in Recipe 18.6 .

17.7 Storing Images or Other Binary Data

17.7.1 Problem

You want t o st ore im ages in MySQL.

17.7.2 Solution

That s not difficult , provided you follow t he proper precaut ions for encoding t he im age dat a.

17.7.3 Discussion

Web sit es are not lim it ed t o displaying t ext . They can also serve various form s of binary dat a such as im ages, sounds, PDF docum ent s, and so fort h. However, im ages are by far t he m ost com m on kind of binary dat a, and because im age st orage is a nat ural applicat ion for a dat abase, a very com m on quest ion is How do I st ore im ages in MySQL? Many people will answer t his quest ion by saying, Dont do it and som e of t he reasons for t his are discussed in t he sidebar Should You St ore I m ages in Your Dat abase? Because it s im port ant t o know how t o work wit h binary dat a, t his sect ion does show how t o st ore im ages in MySQL. Nevert heless, in recognit ion t hat t hat m ay not always be t he best t hing t o do, t he sect ion also shows how t o st ore im ages in t he filesyst em . Alt hough t he discussion here is phrased in t erm s of working wit h im ages, t he principles apply t o any kind of binary dat a, such as PDF files or com pressed t ext . I n fact , t hey apply t o any kind of dat a at all, including t ext ; people t end t o t hink of im ages as special som ehow, but t heyre not . One reason im age st orage confuses people m ore oft en t han does st oring ot her t ypes of inform at ion like t ext st rings or num bers is t hat it s difficult t o t ype in an im age value m anually. For exam ple, you can easily use m ysql t o ent er an INSERT st at em ent t o st ore a num ber like 3.48 or a st ring like Je voudrais une bicyclette rouge , but im ages cont ain binary dat a and it s not easy t o refer t o t hem by value. So you need t o do som et hing else. Your opt ions are: • Use t he LOAD_FILE funct ion. • Writ e a program t hat reads in t he im age file and const ruct s t he proper INSERT query for you. Should You Store Images in Your Database? Deciding where t o st ore im ages is a m at t er of t rade- offs. There are pros and cons whet her you st ore im ages in t he dat abase or in t he filesyst em : • St oring im ages in a dat abase t able bloat s t he t able. Wit h a lot of im ages, youre m ore likely t o approach any lim it s your operat ing syst em places on t able size. On t he ot her hand, if you st ore im ages in t he filesyst em , direct ory lookups m ay becom e slow. To avoid t his, you m ay be able t o im plem ent som e kind of hierarchical st orage or use a filesyst em t hat has good lookup perform ance for large direct ories such as t he Reiser filesyst em . • Using a dat abase localizes st orage for im ages t hat are used across m ult iple web servers on different host s. I m ages st ored in t he filesyst em m ust be st ored locally on t he web server host . I n a m ult iple- host sit uat ion, t hat m eans you m ust replicat e t he set of im ages t o t he filesyst em of each host . I f you st ore t he im ages in MySQL, only one copy of t he im ages is required; each web server can get t he im ages from t he sam e dat abase server. • When im ages are st ored in t he filesyst em , t hey const it ut e in essence a foreign key. I m age m anipulat ion requires t wo operat ions: one in t he dat abase and one in t he filesyst em . This in t urn m eans t hat if you require t ransact ional behavior, it s m ore difficult t o im plem ent —not only do you have t wo operat ions, t hey t ake place in different dom ains. St oring im ages in t he dat abase is sim pler because adding, updat ing, or rem oving an im age requires only a single record operat ion. I t becom es unnecessary t o m ake sure t he im age t able and t he filesyst em rem ain in sync. • I t can be fast er t o serve im ages over t he Web from t he filesyst em t han from t he dat abase, because t he web server it self opens t he file, reads it , and w rit es it t o t he client . I m ages st ored in t he dat abase m ust be read and writ t en t wice. First , t he MySQL server reads t he im age from t he dat abase and writ es it t o your web script . Then t he script reads t he im age and writ es it t o t he client . • I m ages st ored in t he filesyst em can be referred t o direct ly in web pages by m eans of img t ag links t hat point t o t he im age files. I m ages st ored in MySQL m ust be served by a script t hat ret rieves an im age and sends it t o t he client . How ever, even if im ages are st ored in t he filesyst em and accessible t o t he web server, you m ight st ill want t o serve t hem t hrough a script . This would be appropriat e if you need t o account for t he num ber of t im es you serve each im age such as for banner ad displays where you charge cust om ers by t he num ber of ad im pressions or if you want t o select an im age at request t im e such as when you pick an ad at random . • I f you st ore im ages in t he dat abase, you need t o use a colum n t ype such as a BLOB . This is a variable lengt h t ype, so t he t able it self will have variable- lengt h rows. Operat ions on fixed- lengt h rows are oft en quicker, so you m ay gain som e t able lookup speed by st oring im ages in t he filesyst em and using fix- lengt h t ypes for t he colum ns in t he im age t able.

17.7.4 Storing Images with LOAD_FILE