PHP - Allow Only Image To Be Upload

In this tutorial we will create a Allow Only Image To Be Upload using PHP. This code will allow the user to upload any image file to the database server. The code use PHP POST method to initiate a specific function that can upload only image file to the MySQLi server, by using in_array() function by providing the file extension and an array list of image format. This is user-friendly kind of program feel free to modify and apply it to your code.

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 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_allow, 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_allow");
  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="https://sourcecodester.com">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 - Allow Only Image To Be Upload</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-4">
  18.                         <form method="POST" enctype="multipart/form-data">
  19.                                 <div class="form-inline">
  20.                                         <input type="file" name="image"/>
  21.                                         <br />
  22.                                         <center><button class="btn btn-primary btn-sm" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button></center>
  23.                                 </div>
  24.                         </form>
  25.                         <?php include 'upload.php'?>
  26.                 </div>
  27.                 <div class="col-md-8">
  28.                         <div class="table-responsive">
  29.                                 <table class="table table-bordered">
  30.                                         <thead class="alert-info">
  31.                                                 <tr>
  32.                                                         <th>Image</th>
  33.                                                         <th>Image Name</th>
  34.                                                         <th>Date Uploaded</th>
  35.                                                 </tr>
  36.                                         </thead>
  37.                                         <tbody>
  38.                                                 <?php
  39.                                                         require'conn.php';
  40.                                                         $query=mysqli_query($conn, "SELECT * FROM `image` ORDER BY `date_uploaded` ASC") or die(mysqli_error());
  41.                                                         while($fetch=mysqli_fetch_array($query)){
  42.                                                 ?>
  43.                                                 <tr>
  44.                                                         <td><img src="upload/<?php echo $fetch['image_name']?>" height="80" width="120"/></td>
  45.                                                         <td><?php echo $fetch['image_name']?></td>
  46.                                                         <td><?php echo $fetch['date_uploaded']?></td>
  47.                                                 </tr>
  48.                                                 <?php
  49.                                                         }
  50.                                                 ?>
  51.                                         </tbody>
  52.                                 </table>
  53.                         </div>
  54.                 </div>
  55.         </div>
  56. </body>
  57. </html>

Creating the Main Function

This code contains the main function of the application. This code will upload only accept image file. To do that just copy and write this block of codes inside the text editor, then save it as upload.php.
  1. <?php
  2.  
  3.         date_default_timezone_set("Etc/GMT+8");
  4.        
  5.         require_once 'conn.php';
  6.        
  7.         if(ISSET($_POST['upload'])){
  8.                 $file_name = $_FILES['image']['name'];
  9.                 $file_temp = $_FILES['image']['tmp_name'];
  10.                 $date_upload = date("Y-m-d");
  11.                 $allowed_ext = array("jpeg", "jpg", "gif", "png");
  12.                 $exp = explode(".", $file_name);
  13.                 $ext = end($exp);
  14.                 $path = "upload/".$file_name;
  15.                 if(in_array($ext, $allowed_ext)){
  16.                         if(move_uploaded_file($file_temp, $path)){
  17.                                 mysqli_query($conn, "INSERT INTO `image` VALUES('', '$file_name', '$date_upload')") or die(mysqli_error());
  18.                                 echo "<center><h5 class='text-success'>Image uploaded!</h5></center>";
  19.                         }
  20.                 }else{
  21.                         echo "<center><h5 class='text-danger'>Image only is allowed!</h5></center>";
  22.                 }
  23.         }
  24. ?>
There you have it we successfully created Allow Only Image To Be Upload 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