PHP - Download Files With Timer Using SQLite

In this tutorial we will create a Download Files With Timer Using SQLite. This code can be use for storing of list of data. PHP is a server-side scripting language designed primarily for web development. SQLite provides an interface for accessing the database. It includes class interfaces to the SQL commands. And also it allows you to create SQL functions and aggregate using PHP. 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/.

Setting Up SQLite

First, we are going to enable SQLite 3 in our PHP. 1. Open localhost server folder XAMPP, etc and locate php.ini. 2. Open php.ini and enable sqlite3 by removing the semicolon in the line. tut1 3. Save changes and Restart Server

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.        
  3.         $conn = new SQLite3('db/db_download.db');
  4.        
  5.         $query = "CREATE TABLE IF NOT EXISTS file (file_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, filename VARCHAR, location VARCHAR)";
  6.        
  7.         $conn->exec($query);
  8.  
  9. ?>

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>
  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 Timer Using SQLite</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" required="required"/>
  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-success">
  24.                                 <tr>
  25.                                         <th>Filename</th>
  26.                                         <th>Action</th>
  27.                                 </tr>
  28.                         </thead>
  29.                         <tbody style="background-color:#fff;">
  30.                                 <?php
  31.                                         require 'conn.php';
  32.                                        
  33.                                         $query = $conn->query('SELECT * FROM `file`');
  34.                                         while($fetch = $query->fetchArray()){
  35.                                 ?>
  36.                                 <tr>
  37.                                         <td><?php echo $fetch['filename']?></td>
  38.                                         <td><a class="btn btn-primary btn-sm" href="download_page.php?file=<?php echo $fetch['filename']?>"><span class="glyphicon glyphicon-download"></span> Download</a> | <a class="btn btn-danger btn-sm" href="remove.php?file_id=<?php echo $fetch['file_id']?>&file=<?php echo $fetch['filename']?>"></span> Remove</a></td>
  39.                                 </tr>
  40.                                 <?php
  41.                                         }
  42.                                 ?>
  43.                         </tbody>
  44.                 </table>
  45.         </div>
  46. </body>
  47. </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 methods, it can upload the file to the SQLite, and remove file using SQLite 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.         date_default_timezone_set('Asia/Manila');
  3.         require_once 'conn.php';
  4.  
  5.         if(ISSET($_POST['upload'])){
  6.                 $file_name = $_FILES['file']['name'];
  7.                 $file_temp = $_FILES['file']['tmp_name'];
  8.                 $ext = explode('.', $file_name);
  9.                 $name = date("Y-m-d").round(microtime(true)) . substr(md5(rand()), 0, 4).".".$ext[1];
  10.                
  11.                 $location = "files/".$name;
  12.                
  13.                 if(move_uploaded_file($file_temp, $location)){
  14.                         $conn->exec("INSERT INTO `file` (filename, location) VALUES('$name', '$location')");
  15.                         header("location:index.php");
  16.                 }
  17.  
  18.  
  19.         }
  20. ?>
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.                
  9.                 if(unlink('files/'.$file)){
  10.                         $conn->exec("DELETE FROM `file` WHERE `file_id` = '$file_id'");
  11.                         header('location:index.php');
  12.                 }
  13.         }
  14.  
  15. ?>

Creating the Download Function

This code contains the download function of the application. This code will download the selected file by locating the file name from SQLite 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 main function 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 a Download Files With Timer Using SQLite. 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

Thanks for taking the time to do one using sqlite. Kids will love playing with this one. Also good to compare to mysqli version:)

This tutorial was very useful in the classroom. sqLite is perfect for small projects. Maybe in future you could show converting one of your mySQLi tutorials to sqLite using PDO:) Now for question: In the conn.php, how can default table data be added? In other words, inset some default data into table when it is created. If the default data already exist, do not add again or overwrite. There is much info on this but none seem to really work. We have searched and searched on stackoverflow without a solution. Thanks again for your tutorial.

Add new comment