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
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.
1 2 3 4 5 |
CREATE TABLE `images` ( `id` int(11) NOT NULL, `image` varchar(100) NOT NULL, `image_text` text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
<!-- Code By WebDevTrick ( https://webdevtrick.com ) --> <?php $db = mysqli_connect("localhost", "root", "", "image_upload"); $msg = ""; if (isset($_POST['upload'])) { $image = $_FILES['image']['name']; $image_text = mysqli_real_escape_string($db, $_POST['image_text']); $target = "images/".basename($image); $sql = "INSERT INTO images (image, image_text) VALUES ('$image', '$image_text')"; mysqli_query($db, $sql); if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) { $msg = "Image uploaded successfully"; }else{ $msg = "Failed to upload image"; } } $result = mysqli_query($db, "SELECT * FROM images"); ?> <!DOCTYPE html> <html> <head> <title>Image Upload</title> <style type="text/css"> #content{ width: 50%; margin: 20px auto; border: 1px solid #cbcbcb; } form{ width: 50%; margin: 20px auto; } form div{ margin-top: 5px; } #img_div{ width: 80%; padding: 5px; margin: 15px auto; border: 1px solid #cbcbcb; background: #767272; color: white; } #img_div:after{ content: ""; display: block; clear: both; } img{ float: left; margin: 5px; width: 300px; height: 140px; } </style> </head> <body> <div id="content"> <?php while ($row = mysqli_fetch_array($result)) { echo "<div id='img_div'>"; echo "<img src='images/".$row['image']."' >"; echo "<p>".$row['image_text']."</p>"; echo "</div>"; } ?> <form method="POST" action="index.php" enctype="multipart/form-data"> <input type="hidden" name="size" value="1000000"> <div> <input type="file" name="image"> </div> <div> <textarea id="text" cols="40" rows="4" name="image_text" placeholder="What's In Your Mind?"></textarea> </div> <div> <button type="submit" name="upload">Upload</button> </div> </form> </div> </body> </html> |
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.
Nice,
Please consider developing a datepicker in php.
Regards
Suresh
Sure I will post soon.
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.
How to amp blogger website
I face problem on file location
all images located on Images Folder
And how do I display the upload image in PHP?
Try the codes there is also display feature.
pls Shaan, where do I create the “images” folder since I’m using a theme?
Folder name – “upload”
Ok thanks so much
how to update ?
really helpful…thanks
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?
how can i delete image without going into the database?