PHP - Simple Upload Only Image

In this tutorial we will create a Simple Upload Only Image using PHP. PHP is a server-side scripting language designed primarily for web development. It is mostly used by a newly coders for its user friendly environment. So let's now do the coding...

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_save, 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_save");
  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 - Simple Upload Only Image</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <form method="POST" enctype="multipart/form-data">
  18. <div class="form-inline">
  19. <input type="file" name="file" class="form-control"/>
  20. <br />
  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. <br />
  26. <?php include 'upload.php'?>
  27. </div>
  28. </body>
  29. </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.  
  4. require_once 'conn.php';
  5.  
  6. if(ISSET($_POST['upload'])){
  7. $file_name = $_FILES['file']['name'];
  8. $file_temp = $_FILES['file']['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 `upload` VALUES('', '$file_name', '$date_upload')") or die(mysqli_error());
  17. echo "<center><img src='upload/".$file_name."' width='400px' height='400px'/></center>";
  18. }
  19. }else{
  20. echo "<center><h3 class='text-danger'>Only image format can be upload</h3></center>";
  21. }
  22. }
  23. ?>
There you have it we successfully created Simple Upload Only Image 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