PHP - Download Files With Countdown Timer

In this tutorial we will create a Download Files With Countdown Timer using PHP. PHP is a server-side scripting language designed primarily for web development. It is a lean and consistent way to access databases. This means developers can write portable code much easier. 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 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_download, 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_download');
  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 you text editor, then save it as shown below. 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 - Download Files With Countdown Timer</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <form class="form-inline" method="POST" action="upload.php" enctype="multipart/form-data">
  18. <input class="form-control" type="file" name="file"/>
  19. <button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button>
  20. </form>
  21. <br /><br />
  22. <table class="table table-bordered">
  23. <thead class="alert-info">
  24. <tr>
  25. <th>Filename</th>
  26. <th>Location</th>
  27. <th>Action</th>
  28. </tr>
  29. </thead>
  30. <tbody style="background-color:#fff;">
  31. <?php
  32. require 'conn.php';
  33. $query = mysqli_query($conn, "SELECT * FROM `file`") or die(mysqli_error());
  34. while($fetch = mysqli_fetch_array($query)){
  35. ?>
  36. <tr>
  37. <?php
  38. $file = explode('/', $fetch['location']);
  39. ?>
  40. <td><?php echo $fetch['filename']?></td>
  41. <td><?php echo $fetch['location']?></td>
  42. <td><a class="btn btn-success btn-sm" href="download_page.php?file=<?php echo $file[1]?>"><span class="glyphicon glyphicon-download"></span> Download</a> <a class="btn btn-danger btn-sm" href="remove.php?file=<?php echo $file[1]?>&file_id=<?php echo $fetch['file_id']?>"><span class="glyphicon glyphicon-trash"></span> Remove</a></td>
  43. </tr>
  44. <?php
  45. }
  46. ?>
  47. </tbody>
  48. </table>
  49. </div>
  50. </body>
  51. </html>
download_page.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 - Download Files With Countdown Timer</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <a class="btn btn-success" href="index.php">Back</a>
  18. <?php
  19. if(ISSET($_REQUEST['file'])){
  20. ?>
  21. <h4>Your download will start in <span id="timer" class="text-primary">10</span> seconds...</h4>
  22. <input type="hidden" id="file" value="<?php echo $_REQUEST['file']?>"/>
  23. <br /><br />
  24. <h4>Problems with the download? Please use this <a href="download.php?file=<?php echo $_REQUEST['file']?>">direct link</a></h4>.
  25. <?php
  26. }
  27. ?>
  28. </div>
  29. <script src="js/script.js"></script>
  30. </body>
  31. </html>

Creating PHP Query

This code contains the php query of the application. This code has two functionalities, it can upload the file, and remove file via MySQLi query. To do that just copy and write this block of codes inside the text editor, then save it as shown below. upload.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['upload'])){
  5. $file_name = $_FILES['file']['name'];
  6. $file_temp = $_FILES['file']['tmp_name'];
  7. $ext = explode('.', $file_name);
  8. $newFile = $ext[0];
  9. $location = "files/".$file_name;
  10.  
  11. if(move_uploaded_file($file_temp, $location)){
  12. mysqli_query($conn, "INSERT INTO `file` VALUES('', '$newFile', '$location')") or die(mysqli_error());
  13. header("location:index.php");
  14. }
  15.  
  16.  
  17. }
  18. ?>
remove.php
  1. <?php
  2. require_once 'conn.php';
  3. if(ISSET($_REQUEST['file']) && $_REQUEST['file_id']){
  4.  
  5. $file = $_REQUEST['file'];
  6. $file_id = $_REQUEST['file_id'];
  7.  
  8. if(unlink('files/'.$file)){
  9. mysqli_query($conn, "DELETE FROM `file` WHERE `file_id` = '$file_id'") or die(mysqli_error());
  10. header('location:index.php');
  11. }
  12. }
  13.  
  14. ?>

Creating the Download Function

This code contains the download function of the application. This code will download the file that the user selected in the browser. To this just copy and write down these codes inside the text editor, then save it as download.php.
  1. <?php
  2. if(ISSET($_REQUEST['file'])){
  3. $file = $_REQUEST['file'];
  4.  
  5. header("Content-Disposition: attachment; filename=".$file);
  6. header("Content-Type: application/octet-stream;");
  7. readfile("files/".$file);
  8. }
  9. ?>

Creating the Main Function

This code contains the mainfunction of the application. This code will start countdown and automatically download the file after the timer runs out. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. var timer = 10;
  2. var counter;
  3. var file = document.getElementById('file').value
  4.  
  5. function startTimer() {
  6. counter = setInterval(countDown, 1000);
  7. }
  8.  
  9. function countDown() {
  10. var display = document.getElementById("timer");
  11. display.innerHTML = timer;
  12.  
  13.  
  14. timer--;
  15.  
  16. if (timer < 0) {
  17. clearInterval(counter);
  18. window.location.href = "download.php?file="+file;
  19. }
  20. }
  21.  
  22. startTimer();
There you have it we successfully created Download Files With Countdown Timer 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!

Comments

Submitted byoleteacheron Fri, 09/14/2018 - 17:35

Absolutely LOVE your tutorials and use some of them in the classroom. But, it is not possible to create a mysql database for every tiny tutorial. Maybe you could show how to use sqlite also. Not much hope in a response from though since none in the past for other comments:) Anyway, thanks for taking the time to share your knowledge.

You're Welcome, I'm glad that you appreciate my work, Thanks for the suggestion, Anytime soon I will be recreating this tutorial using SQLite. Stay tuned!

Add new comment