Upload an image and store in database using php code

 

Introduction

In this article, step by step we will see php code example to how to upload an image and store that uploaded image path in mysql database using PHP code.

Step 1 : Create a simple HTML form

In step 1, let's create a simple HTML form.

 
<form action="saveimage.php" enctype="multipart/form-data" method="post">

<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody><tr>
<td>
<input name="uploadedimage" type="file">
</td>

</tr>

<tr>
<td>
<input name="Upload Now" type="submit" value="Upload Image">
</td>
</tr>


</tbody></table>

</form>

As you see from our above code that we have created a simple form which consist of input:file and input:submit button.

Make Proper use of Form Attributes

To upload any image file, It is mandatory to set following attribute as, enctype = "multipart/form-data" and action="a target path where form-data will be posted" before submitting it to the server and this is only applicable in HTTP method = "post". These attributes is available in "form" tag. So while uploading any image file make sure you have set those above attributes properly.

Since we will be using PHP scripting language to save an image to the server therefore in the "form-action" attribute we will use file with ".php" extension.

Step 2 : Create MYSQL Database Table

To create a database table, we need to write a mysql query. If you want you can use "phpmyadmin" an interface to create tables or database.

 
CREATE TABLE images_tbl(
   images_id INT NOT NULL AUTO_INCREMENT,
   images_path VARCHAR(200) NOT NULL,
   submission_date DATE,
   PRIMARY KEY (images_id)
);

As you see from above the screen shot that we have successfully created a table called "images_tbl" with columns "images_id" as

Primary Key, "images_path" to save image path and "submission_date" to note the image uploaded date.

Step 3 : Create MYSQL Connection in PHP

We will create a new php file for the connection and lets name it as "mysqlconnect.php". So lets write mysql connection.

Code in "mysqlconnect.php" file.

 

We have used "mysql_connect()" function to connect mysql and "mysql_select_db()" function to connect to particular database.

Step 4 : Store Image in Database using PHP

To store uploaded image path in database we will create a new php file name it as "saveimage.php" because in the "form" tag we gave "action" attribute target path as "saveimage.php" and in this file ("saveimage.php") all the form-data will be posted.

Let's Code "saveimage.php" file.

First lets include the mysql connection file in top of "saveimage.php" file.

 


Second let' write a simple function which will give us the image extension from a image type because there are different image types available to upload like"jpeg, gif, png, bmp" etc. So we have to create a simple function which will return us the image extension depending on the image-type.

 

Kindly Note : If you want to save an image with new name then only use the above function other wise you can also get uploaded image name with extension by using the global PHP $_FILES["uploadedimage"]["name"] variable.

In this example we will be storing an image with new name so we will be using that above "GetImageExtension()" function.

Let's store an Image in database with new name.

Complete Code in "saveimage.php" file to Store an Uploaded Image in Database

 
 ".mysql_error());  
	
}else{

   exit("Error While uploading image on the server");
} 

}

?>;

If you don't want to use the new image name you can replace the following code as shown below.

 


About move_uploaded_file()

move_uploaded_file() a php inbuilt function which helps us to upload any image or file to the server. It takes input as file name and target source destination.

move_uploaded_file (string $filename , string $destination)

If you see in our above that we have called our insert mysql query only when move_uploaded_file() function uploads the file successfully to the server other wise exit with an error.

When move_uploaded_file() upload the file to the server that time we have called mysql insert query on table "images_tbl" to store the image name and path from "$imagename" variable and submission_date as today's server d by using date by using php date() function.

Step 5 : OUTPUT : Display Image From Database using PHP

 



" alt="" />">

This is all about the php image uploading and storing it in the mysql database. If you like this article kindly share it with your friends and want to comment please let me know through you comments. Thanks

 

 

:: SHARE THIS POST ::

        
 

More PHP Articles To Read Next:

 
May
01
2013

How to get Date and Time using PHP

CLICK TO READ FULL ARTICLE

Views - 7515 |Category - PHP

Apr
28
2013

What is PHP with a simple example using PHP

CLICK TO READ FULL ARTICLE

Views - 4377 |Category - PHP

 
Author: Gurunatha Dogi
is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory

be319

 

 
Comments

Posted by Ramalingam P on 2014-10-01

It's very help to learn for beginners.. Thanks

Posted by USL on 2014-09-26

Hi. This script uploads any type of file. Is there a way to restrict it to only image files? Thanks

Posted by Mps on 2014-09-05

i m facing the syntax error on my side.. plz advice

Posted by Muhammad Bilal Haider on 2014-08-30

$query_upload="INSERT into images_tbl ( images_path , submission_date ) VALUES ('".$target_path."','".date("Y-m-d")."')";

Posted by Shakiralikhan on 2014-08-20

mast

Posted by Manju on 2014-08-19

good one.. u deserve a wow..

Posted by Nkosinathi on 2014-08-18

I found your scripts very useful, I will use it for employee signatures. I only found problems with the output code but I think i will figure it out. its not working properly on line /14...

Posted by Michael john pinza on 2014-08-14

how to I fixed these error?? "error on INSERT into 'images_tbl' ('images_path','submission_date') VALUES('img/14-09-2014-1410664188','2014-09-14') == ----> You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''images_tbl' ('images_path','submission_date') VALUES('img/14-09-2014-1410664188' at line 1" PLz help me plz

Posted by Nkosinathi on 2014-08-12

Its great code

Posted by Raj on 2014-08-11

good

Posted by Babloo on 2014-08-09

Data base upload a image in my Project

Posted by Abdul on 2014-08-05

Am having sytnax error at this line

Posted by Sanjay on 2014-08-04

nice

Posted by Ngugi Ndung'u on 2014-07-24

I really love the way you are explaining even befor I have a look on it. I think the site will help me a lot.

Posted by Sadiqa on 2014-07-13

wawo it really helped me in my project thanks buddy .... :)

Posted by KIm on 2014-07-10

Wow !!!! This is just what I was looking for my assignment. Thank you and God bless you. How can you use a simple JSON web service that can be used to retrieve the title, description, filename, width and height of the image that has been uploaded to the application. The data should be returned from the service. Any Idea ? Thanks again.

Posted by Kim on 2014-07-06

does it apply to xamp coz am recieving this error...error in INSERT into 'images_tbl' ('images_path','submission_date') VALUES ('images/06-07-2014-1404644316.bmp','2014-07-06') == ----> You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''images_tbl' ('images_path','submission_date') VALUES ('images/06-07-2014-14' at line 1

Posted by Ab on 2014-07-04

I wanna assure that the image is saved, in which folder by default these images get saved??

Posted by Admin on 2014-07-02

@Gayathri, Sure will do that anyways thanks for posting your comments

Posted by Gayathri on 2014-07-02

I can able to upload the image but while performing edit its not working can u pls send me the code foe edit and update the image ... regards, gayunattu

Posted by Admin on 2014-07-02

@Poojan Shah, Kindly check the folder permission..Folder permission should be 0777 Thanks

Posted by Poojan Shah on 2014-06-30

Hello thank you for your help but the code is not working . While uploading the image I am getting error for move_uploaded_file. kindly help me out.

Posted by Karar Barcha on 2014-06-19

Hello Sir, Please Help me for my project, i am student of CS, so i am working on a project. My project title is "Daily K2" ePaper, (online news paper, using jpg in body). so first of all i need interface which i have designed using html and css, now i need Database (wamp server) and php code for inserting news paper image with date, and also display that jpg with date. i have four pages of news paper with jpg imag in main body which must insert is database in every day by day, and must display with date..... I also need to facility to display previous papers with date...... Please help me in my project sir Am waiting for your Message Karar Barcha (kararbarcha@gmail.com)

Posted by Rajitha Madushan on 2014-06-14

Nice article Guru... great work. it is worked for me...perfectly. good work Carry on..

Posted by Shanavas R on 2014-06-11

Excellent work

Posted by Idris on 2014-06-09

Nice work>>>>>>>>>>>>>>>>> thanks

Posted by Admin on 2014-05-27

@saman....You have used single quote on table and column names instead of single quote use ` grave ascent

Posted by Saman on 2014-05-25

this does not work why??

Posted by Saman on 2014-05-25

error in INSERT into 'images_tbl' ('images_path','submission_date') VALUES ('images/27-05-2014-1401208852.jpg','2014-05-27') == ----> You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''images_tbl' ('images_path','submission_date') VALUES ('images/27-05-2014-140120' at line 1 -----what is the mistake here???? that i have done---

Posted by Henry on 2014-05-08

nice tutorial for uploading picture and save its image in folder not directly in database, thanks mate :)

Posted by Enos mabula on 2014-05-04

I tried to upload image into database but it is returning a warining at line 22 and 29 and it says Error While uploading image on the server so please I need more help

 

Categories