PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Storing Images In MySQL

Article Comments (31 total)

Displaying article comments 21 to 30 of 31
andy, 23 July 2008
i can't get this to work, " method="post" what should the file name of listing-4.php and listing-5.php be to get this to work?

tom, 20 April 1313
1) Shouldn't the column type be MEDIUMBLOB (2^24) for the 10,000,000 bytes in MAX_FILE_SIZE? The MySQL manual says the following limits, in bytes, are imposed: BLOB = 2^16 = 65536 MEDIUMBLOB = 2^24 = 16,777,216 LARGEBLOB = 2^32 = 4,294,967,296 2) In page 8, the line: $sql = "SELECT image FROM testblob WHERE image_id=0"; should say: $sql = "SELECT image FROM testblob WHERE image_id=".$_GET['image_id'];

enoch ssen, 13 May 2008
thanks i found it very helpful but IMHOits best to use pointers besides shortening access time and backing up less tideous, image editing is really possible though i havent figured out how to retrieve file links stored in the database any help is highly appreciated thnx

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.


Wayde, 3 May 2008
After trying a number of different methods to view images stored in a MySql Database, this one worked like a charm. Thanks a lot!!

Joey Marino, 20 April 8839
I can't find a way to display multiple images on the same page with this method. If you loop through a set of blobs referencing the same script with different id's, the script changes multiple times over and all that gets displayed repetitively is the last image in the set.

Halfasix, 11 March 2008
I have done this project over and over again at least 34 times and i cant get this project to work... in fact i cant find a simple upload picture to database and view picture from database project that works... i have located at least 60 scripts and i cant get none of them to work...im sure it cant be that difficult.. In this paticular tutorial nothing works...my question is simply this! all the scripts given in this tutorial are not quite clear as to how to use them...for eg... do i join them together to make one big script? or does each script link from inside the script...coz i cant see how Listing-3.php links or reads from listing-4.php and so forth.. it just looks like a bunch of independant files that do absoloutely nothing on there own. how about a idiot proof guide on how to do this including all the software version details coz im using php5 (vertrigo wamp)where as alot of these scripts are done in a really old version of php. the authors need to tell us there software specifications on developing the tutorial so we learners dont waste our time.

Scott Baetz, 8 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.


Andreas Christodoulou, 22 January 2007
Hi I have tried your code and I get the following error: File exceeds the Maximum File limit Maximum File limit is 10000000 File Array is Array bytes ------------------------------------- Thank you for submitting I tried to upload different size of images but I still get this error. Can you please tell me what I am doing wrong? Thanks

ben, 14 January 2007
hi! this wont be any wear near as interesting as the above conversation. i am a beginner - intermediate when it comes to PHP and SQL. i have followed the steps in of this article, but for some reason, the images dont show. it keeps returing the else statement of 'please use a real ID number' on view.php. i have tried to change the tinyint for the image_id field on the SQL table to 5, and uploaded a file of 5 or less digits, but to no avail. any help will be much appreciated!
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


Article History

Apr 20, 9804
Initial article version
Jan 18, 2008
Updated listing 5 to not include non-existent conf.php