Easy and Simple way to Upload Image using PHP/MySQLi

This tutorial will show you how to create a simple image uploader using PHP/MySQLi. This tutorial does not include a good design but will give you an idea of how to upload images using PHP/MySQLi. Our goal in this tutorial is to create an Image Upload function into our simple Web App and Display the images after saving them into the database. The input that we will be using is an input file that only accepts any types of images.

Getting Starte

Please download and install the ff software if not yet installed in your desktop or laptop.

Creating our Database

First, we're going to create a database that will store the location of our image.

  1. Open PHPMyAdmin.
  2. Click databases, create a database and name it as image_upload.
  3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
  1. CREATE TABLE IF NOT EXISTS `image_tb` (
  2. `imageid` int(11) NOT NULL AUTO_INCREMENT,
  3. `img_location` varchar(150) NOT NULL,
  4. PRIMARY KEY (`imageid`)

Creating our Connection

Next, we create a database connection and save it as conn.php. This file will serve as our bridge between our form and our database.

  1. <?php
  2. $con = mysqli_connect("localhost","root","","image_upload");
  4. // Check connection
  5. {
  6. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  7. }
  8. ?>

Creating our Output Folder

Next step is to create a folder that will store our uploaded images and name it as upload.

Creating our Form

Next step is to create our form and save it as index.php. This is also the place where we can see our uploaded images. To create the form, open your HTML code editor and paste the code below after the tag.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Easy and Simple Image Upload</title>
  6. <style>
  7. .container{
  8. display: flex;
  9. width:calc(100%);
  10. flex-wrap: wrap;
  11. }
  12. .container img{
  13. width: calc(18%);
  14. height: 15vw;
  15. object-fit: contain;
  16. background: gray;
  17. border: 1px solid black;
  18. margin: 5px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  24. <h2><strong>Uploaded Images:</strong></h2>
  25. <div class="container">
  26. <br>
  27. <?php
  28. include('conn.php');
  29. $query=mysqli_query($con,"select * from image_tb");
  30. if(mysqli_num_rows($query) > 0){
  31. while($row=mysqli_fetch_array($query)){
  32. ?>
  33. <img src="<?php echo $row['img_location']; ?>">
  34. <?php
  35. }
  36. }else{
  37. echo "<p><strong>No Images uploaded yet.</strong></p>";
  38. }
  39. ?>
  40. </div>
  41. <div>
  42. <form method="POST" action="upload.php" enctype="multipart/form-data">
  43. <label>Image:</label><input type="file" name="image" accept="image/*">
  44. <button type="submit">Upload</button>
  45. </form>
  46. </div>
  47. </body>
  48. </html>

Writing our Upload Script

Lastly, we create a script that will save our uploaded images and name it as upload.php.

  1. <?php
  2. include('conn.php');
  3. if(!empty($_FILES["image"]["tmp_name"])){
  4. $fileinfo=PATHINFO($_FILES["image"]["name"]);
  5. $newFilename=$fileinfo['filename'] ."_". time() . "." . $fileinfo['extension'];
  6. move_uploaded_file($_FILES["image"]["tmp_name"],"upload/" . $newFilename);
  7. $location="upload/" . $newFilename;
  9. mysqli_query($con,"insert into image_tb (img_location) values ('$location')");
  10. header('location:index.php');
  11. }else{
  12. echo "<script>alert('No Image selected.');location.replace('./index.php');</script>";
  13. }
  15. ?>

That's it! Now test your work if it is working as it is. If not, try to repeat the tutorial from the start or you may download the working source code that I uploaded with this tutorial. Feel free to download, test, and differentiate it with your work so you will know which part you have missed.

That's the end of this tutorial. I hope this tutorial helps you with what you are looking for and I hope this helps you to learn something useful for your future projects.

Happy Coding :)


Submitted bykimenyi fabrice (not verified)on Sat, 08/17/2019 - 20:18

wowww thx bro it worked where have you been
Submitted byAthbsbs (not verified)on Thu, 11/03/2022 - 13:15


Add new comment