PHP - Download Files With Countdown Timer

Submitted by: 
Language: 
Visitors have accessed this post 352 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">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 - Download Files With Countdown Timer</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <form class="form-inline" method="POST" action="upload.php" enctype="multipart/form-data">
  19. <input class="form-control" type="file" name="file"/>
  20. <button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button>
  21. </form>
  22. <br /><br />
  23. <table class="table table-bordered">
  24. <thead class="alert-info">
  25. <tr>
  26. <th>Filename</th>
  27. <th>Location</th>
  28. <th>Action</th>
  29. </tr>
  30. </thead>
  31. <tbody style="background-color:#fff;">
  32. <?php
  33. require 'conn.php';
  34. $query = mysqli_query($conn, "SELECT * FROM `file`") or die(mysqli_error());
  35. while($fetch = mysqli_fetch_array($query)){
  36. ?>
  37. <tr>
  38. <?php
  39. $file = explode('/', $fetch['location']);
  40. ?>
  41. <td><?php echo $fetch['filename']?></td>
  42. <td><?php echo $fetch['location']?></td>
  43. <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>
  44. </tr>
  45. <?php
  46. }
  47. ?>
  48. </tbody>
  49. </table>
  50. </div>
  51. </body>
  52. </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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">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 - Download Files With Countdown Timer</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <a class="btn btn-success" href="index.php">Back</a>
  19. <?php
  20. if(ISSET($_REQUEST['file'])){
  21. ?>
  22. <h4>Your download will start in <span id="timer" class="text-primary">10</span> seconds...</h4>
  23. <input type="hidden" id="file" value="<?php echo $_REQUEST['file']?>"/>
  24. <br /><br />
  25. <h4>Problems with the download? Please use this <a href="download.php?file=<?php echo $_REQUEST['file']?>">direct link</a></h4>.
  26. <?php
  27. }
  28. ?>
  29. </div>
  30. <script src="js/script.js"></script>
  31. </body>
  32. </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

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!

Appreciate your tutorials much:)

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