This tutorial covers a simple way to store images in a mysql database. The example scripts that I provide here are intended to show you how to get a single image into your database. It will be up to you to write other scripts using php loops -- and functions like:
This tutorial includes a fictitious database and table. Lets call the database test_imgs with a single table called pictures.
First let's write a function to connect to the Mysql database
I named my function "database connect" for obvious reasons, then passed the following arguments:
Arguments 1 through 4 need no explanation. Five and six were passed by reference which allows me to retrieve the variable values outside the function. Comment out the "die()" functions when you are not troubleshooting your code.
Now lets get down to the task of storing images in a mysql database.
Notice that I used "fopen(), fread(), and fclose()" to get the contents of "testpic.php" and store it in a variable -- $img.
Next, I take $img and run it through "base64_encode().Read about base64 here.
Finally, after a little SQL magic, I place the encoded data from my test picture into the database. Viola! Now I have a jpeg image stored in mysql. Make note that I made a point to record the file extension along with the picture itself. I will be using that information when I am ready to get images out of my sql database.