Learn PHP
Site Map
Php Functions
C++ - Starting Out
Web Design Tools
Photoshop Tutorials
Droid Phones
Building Websites
ArrowTech Blog
Web Servers
Making Money
Favorite Links
Privacy Policy

Category: php


Storing Images in Mysql Database

Tutorial Outline:
  1. Creating a Test Mysql Database
  2. Example php Database Function
  3. Explanation of Database Function
  4. Php Script to Load Images in Mysql Database
  5. Explanation of Image Script
See Also:
  1. How to Get Images Out of MySql Database
  2. PHP Manual "base64_decode()"
  3. PHP Manual "chunk_split()"
  4. PHP Manual "convert_uuencode()"
  5. PHP Manual "RFC 2045 section 6.8"

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:

Creating a Test Mysql Database

This tutorial includes a fictitious database and table. Lets call the database test_imgs with a single table called pictures.

id pics ext gender
1 retergfghfghterhgcfnt
jpg female
  1. "id" is primary, key, not null, and auto_increment
  2. "pics" blob not null
  3. "ext" is varchar(4) not null
  4. "gender" is varchar(7) not null
  5. Mysql defaults for everything else.
  6. Create a directory "test" for the scritps
  7. Practice script "test.php"

Example Php Database Function

First let's write a function to connect to the Mysql database

Explanation of Database Function

I named my function "database connect" for obvious reasons, then passed the following arguments:

  1. $db_host -- Database Host
  2. $db_user -- Database User Name
  3. $db_pw -- Database Password
  4. $db_name -- Database Name
  5. &$db_selected -- Passed by reference
  6. &$connection -- Passed by reference

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.

Php Script to Load Images in Mysql Database

Now lets get down to the task of storing images in a mysql database.

Explanation of Image Script

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.

Author: D.Shaun Morgan

convert timestamp to readable date time using php Convert Unix Timestamp to Readable Date Time PHP User Function string function convert_timestamp(string $file_in

creating image watermarks Creating PHP Watermarks $my_image = imagecreatefromjpeg('photo.jpeg'); $watermark = imagecreatetruecolor(100

need to print out an array using php How toPrint PHP Arrays Things to Consider Are: Array Type- Single Array or Multidimensional Array


odd number php script Testing for Odd/Even Numbers - PHP Example Banner Rotation PHP Script Odd/Even Numbers The easiest way to setup an Odd / Even test using PHP i

how to upload images and files using php Upload Images to Webserver HTML Form PHP Function # UPLOAD FILE function upload_file(){ echo '

using php cookies variables Using Cookie Variables in PHP Setting cookies in php is pretty straight forward. There is a php function: setcookie(); This function mu

how to unzip files on your free hosting account Uzipping Files on Your Web Server If you are using a free hosting account with godaddy.com and many of the other Free hosts out there, you may have found that they don't offer you a lot o

passing data from one form to another form using php Passing data from one form to another form To pass php data from a form and then catch that data into another form one way to accomplish this task is to intermi

Just In Time Compiling JIT - Just in Time Compilers JIT refers to a special software that is found in web-browsers, and scripting engines such PHP's Zend. (My understanding of ZEND is that it

Storing image locations mysql Store Image URL in Database Retrieve Image with PHP Script

how to turn variable value into variable name Variable Variables in PHP Every so often when you are writing a php file, we all run into a situation where we would like for the name of the string that is inside

what-is-php Explanation of PHP Basic Syntax --New to PHP Php is a language that is very much like writing c. The best feature of PHP versus c is that it is designed pri

how to use dreamweaver Dreamweaver - WISIWIG Dreamweaver is a tool that arguably could be called the industry standard for web design . Dreamweaver, is an Adobe product that originally b

Filezilla Filezilla Ftp Client, FTP SERVER vs Wsftp By: D.Shaun Morgan While I was writing this article, about halfway through, I realized that it would be unfair to tell my readers about

notepad plus Notepad++ Download Notepad Plus Plus Notepad plus plus is an open source, free text editor. It is

how to open create write files using php Php file functions, Opening a file in php, reading a file in php, writing a file in php In this tutorial we will look at the most commonly used file functions used in php. The

stop form spam captcha PHP -- How to Protect My Email Form / Contact Form Against Spam Bots Author: D.Shaun Morgan Versions and Skill Level PHP 5

retreive images mysql using php How to get images out of mysql database with php and use them on my webpages Author: D.Shaun Morgan PHP Version - PHP 5x

how to make money online Making Money on the Internet Sign up for this series of free money making articles Name:

store images mysql using php Storing Images in Mysql Database Tutorial Outline: Creating a Test Mysql Database Example php Database Functio

php example post array Visual example of how PHP handles the super-global array variable "$_POST" Author: D.Shaun Morgan Versions and Skill Level

php arrays Passing PHP Arrays How to pass php variable arrays between pages. Author:

how to write php functions Explanation of a Basic PHP Function What is a PHP function? In PHP a function represents a block of instructions that perform

how to write html form using php When learning PHP to build a website, one of the very first things that any newcomer, or beginner needs to learn is how to use HTML or XHTML (both will mean the same for this tutorial.) fo

register globals long arrays Security, php.ini, register_globals and register_long_arrays Author: D.Shaun Morgan Versions and Skill Level PHP Version - PHP 5x

how to install php 5 How to Install PHP 5.x.x.x on Windows This is beginner's how-to manual explaining how to quickly setup a PHP 5 installation on a Windows PC. You must have an Apache webserver installed

php passing variables Pass Variables from One Page to Another -- PHP Author: D.Shaun Morgan Tutorial Outline : Passing PHP variables with $_


Leave a Comment


Name:      | Email:      | Website:



Enter The Above Security Code:

Author: Taz | Website URL: |
say if I'm trying to make a database in mysql that stores my products with a picture of each product...so when a customer searches for products the info comes up with a small photo next to it...what is the easiest or simplest way this can be achieved??

Re: Admin
A simple way to do this is to create a table for for products and include field in that table that stores teh URL or file location of the picture for that product. This will allow you to store the images in the filesystem instead of in the database. Just pass along the file location to the html for images -- img src --. You could also add a field for the image's thumbnail location or take care of it in your php script.
Author: Shaun | Website URL: http://shaunmorganonline.com |
Great tutorials!
Author: rabi | Website URL: |
I love this tutorial! I was looking everywhere for this information! Finally I found it here.
Author: fatima | Website URL: |
how I can store image in DB and recieve from DB and show it?

Re: Admin

Images and Database
Author: Dani | Website URL: |
Can you tell me how to insert more then one images in database?and how to store them in database? need a row for each file?

Re: Admin

If you use the image methods found at mrarrowhead.com/index.php?page=store_images_mysql_php.php , you should be able to incorporate some type of loop such a for() or while() or do{} while(), etcetera.
Author: gayathri | Website URL: |
I have created a database table with two fields year type: area, yeild
when we select year and area and click submit we should get the image acording to those files how connect the images according to the fields when i select
show the

Re: Admin

Please explain a little more in depth about your problem. Are you having trouble getting the images into or out of the database, or are you haveing trouble writing a program that can return the exact images that you are requesting?
Author: hillary | Website URL: http://www.emarkepedia.com |
nice....thanks a lot
Author: Polin | Website URL: http://www.banglanewspapers.net |
This is good but i have need more like image size and type restrictions and show with html and php. If possible please add more code. Thanks
Author: baluku mzee | Website URL: www.stlawrenceuniversity.ac.ug |
how to store images in mysl database using php and html, also automatic addation, substraction, making automatic calculation in above like after entering all products in the database then the total quantity sums up itself
Author: Kabir Ahmad | Website URL: |
plz tell me the way to save images along in the file and store its reference in database. plz also write the code. thanks
Author: Edgardo | Website URL: http://expertdecoders.com |
Woah I m really digging the template theme of this blog. It s simple, yet effective. A lot of times it s tough to get that perfect balance between user friendliness and visual appearance. I must say you ve done a awesome job with this. Additionally, the blog loads super fast for me on Safari. Excellent Blog
Author: kelechi | Website URL: |
1. please i have been trying to creat a social site where friends can view the comment of only those he she has as friend on the database. how do i manipulate the database so i can get the result using php on a local wampserver..
2. how do i terminate a session using php when a user just close a page without logging out.

please i will like you to send the reply to my email. thamks