Skip to main content

PHP - Image Upload With Gallery

In this tutorial we will create a Image Upload With Gallery using PHP. This code will automatically arrange the uploaded image into a gallery style format. The code use MySQLi SELECT parameter to establish a gallery setup page by looping the images in the database server. This a user-friendly program feel free to modify and use it to your system.

We will be using PHP as a scripting language that interpret in the webserver such as xamp, wamp, etc. It is widely use by modern website application to handle and protect user confidential information.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server

And this is the link for the jquery that i used in this tutorial

Lastly, this is the link for the bootstrap that i used for the layout design

Creating Database

Open your database web server then create a database name in it db_grade, after that click Import then locate the database file inside the folder of the application then click ok.

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn = mysqli_connect("localhost", "root", "", "db_gallery");
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Image Upload With Gallery</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <form method="POST" action="upload.php" enctype="multipart/form-data">
  18. <div class="form-inline">
  19. <label>Upload here</label>
  20. <input type="file" name="image" class="form-control" required="required"/>
  21. <button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button>
  22. </div>
  23. </form>
  24. <br />
  25. <div class="alert alert-info">My Gallery</div>
  26. <?php
  27. require 'conn.php';
  29. $query = mysqli_query($conn, "SELECT * FROM `image`") or die(mysqli_error());
  30. while($fetch = mysqli_fetch_array($query)){
  31. ?>
  32. <div style="border:1px solid #000; height:190px; width:190px; padding:4px; float:left; margin:10px;">
  33. <a href="<?php echo $fetch['location']?>"><img src="<?php echo $fetch['location']?>" width="180" height="180"/></a>
  34. </div>
  35. <?php
  36. }
  37. ?>
  38. </div>
  39. </body>
  40. </html>

Creating PHP Query

This code contains the php query of the application. This code will upload and display the images when the button is clicked. To do that just copy and write this block of codes inside the text editor, then save it as upload.php.

  1. <?php
  2. require_once 'conn.php';
  4. if(ISSET($_POST['upload'])){
  5. $image_name = $_FILES['image']['name'];
  6. $image_temp = $_FILES['image']['tmp_name'];
  7. $image_size = $_FILES['image']['size'];
  8. $ext = explode(".", $image_name);
  9. $end = end($ext);
  10. $allowed_ext = array("jpg", "jpeg", "gif", "png");
  11. $name = time().".".$end;
  12. $path = "upload/".$name;
  13. if(in_array($end, $allowed_ext)){
  14. if($image_size > 5242880){
  15. echo "<script>alert('File too large!')</script>";
  16. echo "<script>window.location = 'index.php'</script>";
  17. }else{
  18. if(move_uploaded_file($image_temp, $path)){
  19. mysqli_query($conn, "INSERT INTO `image` VALUES('', '$name', '$path')") or die(mysqli_error());
  20. echo "<script>alert('Image uploaded!')</script>";
  21. echo "<script>window.location = 'index.php'</script>";
  22. }
  23. }
  24. }else{
  25. echo "<script>alert('Invalid image format!')</script>";
  26. echo "<script>window.location = 'index.php'</script>";
  27. }
  30. }
  31. ?>

There you have it we successfully created Image Upload With Gallery using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.