PHP - Image Upload With Gallery

Submitted by: 
Language: 
Visitors have accessed this post 580 times.

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 https://www.apachefriends.org/index.html.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

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.
tut1

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");
  3.  
  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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Image Upload With Gallery</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <form method="POST" action="upload.php" enctype="multipart/form-data">
  19. <div class="form-inline">
  20. <label>Upload here</label>
  21. <input type="file" name="image" class="form-control" required="required"/>
  22. <button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button>
  23. </div>
  24. </form>
  25. <br />
  26. <div class="alert alert-info">My Gallery</div>
  27. <?php
  28. require 'conn.php';
  29.  
  30. $query = mysqli_query($conn, "SELECT * FROM `image`") or die(mysqli_error());
  31. while($fetch = mysqli_fetch_array($query)){
  32. ?>
  33. <div style="border:1px solid #000; height:190px; width:190px; padding:4px; float:left; margin:10px;">
  34. <a href="<?php echo $fetch['location']?>"><img src="<?php echo $fetch['location']?>" width="180" height="180"/></a>
  35. </div>
  36. <?php
  37. }
  38. ?>
  39. </div>
  40. </body>
  41. </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';
  3.  
  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. }
  28.  
  29.  
  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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.