image upload in php

Upload Image In PHP, With MySQL Database: Nowadays we are on social networks, and we upload DP Stories etc. Ever wondered how this photo is uploaded in the database. After that, we can see images which are coming out of the database. So, Today we will learn how to upload image in database using PHP & MySQL.  Basically, In this tutorial, we will create a form with two inputs. First, for image upload, the second one is to write some text with an image. When someone selects an image write some text & click on the upload button, the image name and text will be upload in MySQL database. And PHP grabs image file and save it in a folder. 

You May Also Like

Build a Simple Quiz In PHP | Source Code

Domain Availability Checker In PHP | Get Source Code

More.

Upload Image In PHP Tutorial

Firstly,  Create a MySQL database named “image_upload” & create a table named “images”.  You can give a custom name for database and tables but, then you have to changes database & table name in PHP file too.  If you have good knowledge in PHP then it’s ok Otherwise, just follow my steps.

Create a database named “image_upload” & click on the database you have created now. Then go to SQL menu and paste these code & click on go.

Now you have successfully created a database for this program. Now Create a file named “index.php” & a folder named “images” in the same destination where you saved index.php file. Now let me explain some important things about this program: I had created a form with method=”POST” and put on action index.php (action=”index.php”) file. You can create another file for PHP I had put everything in a single file. More info about PHP Method.

Note: Make sure you put enctype=”multipart/form-data” in form. Example ( <form method=”POST” action=”index.php” enctype=”multipart/form-data”> ), And don’t forget to create “images” folder. Otherwise, this program will not work.

Source Code

Open “index.php” file you had created before and paste these codes give below:

That’s It. Now you have successfully created image upload in PHP & MySQL program. This a very basic program, I created this for those people who are beginners and want to learn. In the future, I will share advance PHP programs also. If You have any question or doubt comment down below.

Thanks For Visiting, Keep Visiting.

15 COMMENTS

      • Hi Shaan

        I recently upgraded the server with MySQL 5.7. This caused some issues with uploading files and posting to sql db. When I saw your article I had to try it but, although it uploads the file to the directory it doesn’t post to the db. And there’s no error message. Are you able to shed some light on this.

  1. how do you delete an image without having to go into the database? is there a code you could implement to have the option to delete image?

LEAVE A REPLY

Please enter your comment!
Please enter your name here