PhpRiot
Download This Article
Download this article in PDF format with all listings and files.

Price: $5.00 AUD
(Approx. $4.45 USD)

More information
Related Books
The Essential Guide to Dreamweaver CS3 with CSS, Ajax, and PHP

The Essential Guide to Dreamweaver CS3 with CSS, Ajax, and PHP

With over 3 million users worldwide, Adobe's Dreamweaver is the most popular web development...

PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide

PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide

It hasn't taken Web developers long to discover that when it comes to creating dynamic,...
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (12), JSON (2), MVC (1), MySQL (7), onbeforeunload (1), OOP (1), PHP (28), PhpDoc (1), PostgreSQL (6), Prototype (11), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

Comments

Article Comments (3 total)

Joseph Riopelle, 6 May 2008

Richard@Home, I have been using this method for some time now and I have had no problems. The tradeoff between this and a direcory scheme is one that I will accept. I have had no problems with the speed in a script similar to this (although I configured my SQL tables as best as I found possible).

The one thing i have to say about this is that is easier to secure. just throw down mysql_real_escape_string() and it is pretty hard for anyone to upload a malicious image (ie. PHP embedded GIFs) because the PHP is handling the headers and outputting the file before there is the chance to execute anything else.

If you are using plain old directory storage, your file sits on the webroot or wherever and can be directly downloaded via the browser. I tend to let PHP handle all my files anyways, and something like this could be integrated into an access control script, or a hotlink blocker, etc. very easily.

I have to say, so far MySQL images ftw. Great article Kevin; shoot me an email if you want to improve upon it a little, as I may have a few suggestions.


Richard@Home, 26 April 2006

Sorry, still don't agree. It's still a bad idea. By all means store the meta data about an image (with a field for storing the filename) in a database because that's what the users are going to be searching for. Let's have a look at your benefits one by one:

Referential Integrity

This is the only benefit as far as I can see, and its a pretty tenuous one at that. It's pretty trivial to right some code that checks periodically if an image in the database has gone missing.

Ease of Backup

As opposed to what? The regular backup you perform of your website/application anyway?

Saving of Inodes

Dubious to say the least. I've created image directories with hundreds of thousands of images in with no problems. In fact, storing the images as files is better because if you need to increase the space for the images you just add a new hard drive and symbolically link the new directory.

Easy Categorisation

Which is just as easy as storing a link to the file along with its meta data.

Central point of operations

No, because now your image editors have to add another step to the process. Instead of just opening up an image in an image editor, making a change and saving it, they now have to upload it too. With all the problems of browser time-outs to complicate things even further. Also, you have to go to the additional step of pulling the (by definition, large) binary data out of the database, squirting it into an output file with the right headers and then sending the file to the browser. That's one more database query that could bring a busy website to its knees.


Scott Baetz, 7 November 2007

Hey, regardless of the debate, your page provided me an answer when I couldn't choose. My data was from a third party, they store it in an MS SQL database, and for the life of me I couldn't figure out how to post the image and view on a page. Thanks, thanks, thanks.

Submit a Comment
Use the following form to submit a comment for this article. You can include any required code snippets, which we will format and highlight accordingly.


Note: Before your comment appears on PhpRiot, it must be manually approved. The email address field is optional. If you choose to include it, it will be displayed obfuscated to protect it from spammers.

In This Article


Tagged in ,

Article History

May 30, 2005
Initial article version
Jan 18, 2008
Updated listing 5 to not include non-existent conf.php