Skip to main content

PHP - Simple File Upload With Progress Bar

Screenshot
Body
In this tutorial we will create a Simple File Upload With Progress Bar Using jQuery. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's 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 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/. Additionally, this is the link for jQuery Form module http://malsup.com/jquery/form/#download. Note: You need to download this module to make the progress bar work on your application. Make sure you put the jquery.form.js in your HTML file.

Creating Database

Open your database web server then create a database name in it db_upload, 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_upload");
  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. <style type="text/css">
  7. #progress-bar {
  8. background-color: green;
  9. width:0%;
  10. height:20px;
  11. -webkit-transition: width .3s;
  12. -moz-transition: width .3s;
  13. transition: width .3s;
  14. }
  15. #progress-div {
  16. border:#ccc 1px solid;
  17. margin:30px 0px;
  18. border-radius:4px;
  19. text-align:center;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <nav class="navbar navbar-default">
  25. <div class="container-fluid">
  26. <a class="navbar navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  27. </div>
  28. </nav>
  29. <div class="col-md-3"></div>
  30. <div class="col-md-6 well">
  31. <h3 class="text-primary">PHP - Simple File Upload With Progress Bar</h3>
  32. <hr style="border-top:1px dotted #ccc;"/>
  33. <form id="upload_form" action="upload.php" method="post">
  34. <center>
  35. <div class="form-inline">
  36. <input name="upload" id="upload" type="file" class="form-control"/>
  37. <button "btnSubmit" class="btn btn-primary" class="form-control">Upload</button>
  38. </div>
  39. </center>
  40. <div id="progress-div">
  41. <div id="progress-bar"></div>
  42. </div>
  43. <div id="targetLayer" style="display:none;"></div>
  44. </form>
  45. </div>
  46. <script src="js/jquery-3.2.1.min.js"></script>
  47. <script src="js/jquery.form.js"></script>
  48. <script src="js/script.js"></script>
  49. </body>
  50. </html>

Creating PHP Query

This code contains the php query of the application. This code will upload the file to the database server. 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($_FILES['upload'])){
  5. $file_name = $_FILES['upload']['name'];
  6. $file_temp = $_FILES['upload']['tmp_name'];
  7. $path = "upload/".$file_name;
  8.  
  9. if(move_uploaded_file($file_temp, $path)){
  10. mysqli_query($conn, "INSERT INTO `file` VALUES('', '$file_name', '$path')") or die(mysqli_error());
  11. echo "<img src=".$path." height='200' width='200'>";
  12. }
  13. }else{
  14. header('location: index.php');
  15. }
  16. ?>

Creating the Main Function

This code contains the main function of the application. This code will upload the file and then display the progress as a bar animation. To make this just copy and write these block of codes below inside the text editor, then save it as script.js inside the js folder.
  1. $(document).ready(function() {
  2. $('#upload_form').submit(function(e) {
  3. if($('#upload').val() != "") {
  4. e.preventDefault();
  5. $('#targetLayer').hide();
  6. $(this).ajaxSubmit({
  7. target: '#targetLayer',
  8. beforeSubmit: function() {
  9. $("#progress-bar").width('0%');
  10. },
  11. uploadProgress: function (event, position, total, percentageComplete){
  12. $("#progress-bar").width(percentageComplete + '%');
  13. $("#progress-bar").html("<label style='color:#fff;'>Complete</label>");
  14. },
  15. success:function(){
  16. $('#targetLayer').show();
  17. },
  18. resetForm: true
  19. });
  20. return false;
  21. }
  22. });
  23. });
There you have it we successfully created a Simple File Upload With Progress Bar Using jQuery. 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