PHP - Simple Upload Only Image

Submitted by: 
Visitors have accessed this post 353 times.

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

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_save, 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_save");
  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 - Simple Upload Only Image</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <form method="POST" enctype="multipart/form-data">
  19. <div class="form-inline">
  20. <input type="file" name="file" class="form-control"/>
  21. <br />
  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. <br />
  27. <?php include 'upload.php'?>
  28. </div>
  29. </body>
  30. </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['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

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.