Image Upload With Gallery in PHP Tutorial

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 is a user-friendly program feel free to modify and use it in your system.

We will be using PHP as a scripting language that interprets in the web server such as XAMPP, WAMP, etc. It is widely used by modern website applications 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

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_gallery, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Or, you can simple copy/paste the SQL script below to created the table and its column. To do this, navigate your Database to the SQL Tab on the PHPMyAdmin and paste the script in the provided text field and click the Go button.

  1. CREATE TABLE `image` (
  2. `image` varchar(100) NOT NULL,
  3. `location` varchar(100) NOT NULL

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. if(!is_dir("upload/"))
  13. mkdir("upload/");
  14. $path = "upload/".$name;
  15. if(in_array($end, $allowed_ext)){
  16. if($image_size > 5242880){
  17. echo "<script>alert('File too large!')</script>";
  18. echo "<script>window.location = 'index.php'</script>";
  19. }else{
  20. if(move_uploaded_file($image_temp, $path)){
  21. mysqli_query($conn, "INSERT INTO `image` VALUES('', '$name', '$path')") or die(mysqli_error());
  22. echo "<script>alert('Image uploaded!')</script>";
  23. echo "<script>window.location = 'index.php'</script>";
  24. }
  25. }
  26. }else{
  27. echo "<script>alert('Invalid image format!')</script>";
  28. echo "<script>window.location = 'index.php'</script>";
  29. }
  32. }
  33. ?>

DEMO Video

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

Enjoy Coding!

Add new comment