PHP - Allow Only Image To Be Upload

Submitted by: 
Visitors have accessed this post 330 times.

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

And, 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_allow, 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_allow");
  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="">Sourcecodester</a>
  11. " rel="nofollow">">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 - Allow Only Image To Be Upload</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-4">
  19. <form method="POST" enctype="multipart/form-data">
  20. <div class="form-inline">
  21. <input type="file" name="image"/>
  22. <br />
  23. <center><button class="btn btn-primary btn-sm" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button></center>
  24. </div>
  25. </form>
  26. <?php include 'upload.php'?>
  27. </div>
  28. <div class="col-md-8">
  29. <div class="table-responsive">
  30. <table class="table table-bordered">
  31. <thead class="alert-info">
  32. <tr>
  33. <th>Image</th>
  34. <th>Image Name</th>
  35. <th>Date Uploaded</th>
  36. </tr>
  37. </thead>
  38. <tbody>
  39. <?php
  40. require'conn.php';
  41. $query=mysqli_query($conn, "SELECT * FROM `image` ORDER BY `date_uploaded` ASC") or die(mysqli_error());
  42. while($fetch=mysqli_fetch_array($query)){
  43. ?>
  44. <tr>
  45. <td><img src="upload/<?php echo $fetch['image_name']?>" height="80" width="120"/></td>
  46. <td><?php echo $fetch['image_name']?></td>
  47. <td><?php echo $fetch['date_uploaded']?></td>
  48. </tr>
  49. <?php
  50. }
  51. ?>
  52. </tbody>
  53. </table>
  54. </div>
  55. </div>
  56. </div>
  57. </body>
  58. </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
  4. require_once 'conn.php';
  6. if(ISSET($_POST['upload'])){
  7. $file_name = $_FILES['image']['name'];
  8. $file_temp = $_FILES['image']['tmp_name'];
  9. $date_upload = date("Y-m-d");
  10. $allowed_ext = array("jpeg", "jpg", "gif", "png");
  11. $exp = explode(".", $file_name);
  12. $ext = end($exp);
  13. $path = "upload/".$file_name;
  14. if(in_array($ext, $allowed_ext)){
  15. if(move_uploaded_file($file_temp, $path)){
  16. mysqli_query($conn, "INSERT INTO `image` VALUES('', '$file_name', '$date_upload')") or die(mysqli_error());
  17. echo "<center><h5 class='text-success'>Image uploaded!</h5></center>";
  18. }
  19. }else{
  20. echo "<center><h5 class='text-danger'>Image only is allowed!</h5></center>";
  21. }
  22. }
  23. ?>

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

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.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.