PHP - Simple Force Download File

In this tutorial we will create a Simple Force Download File using PHP. This code will forcefully download the uploaded file list when the user click the download button. The code use PHP header to disposition the file content in the database server, and to be able to force download a file. This is a user-friendly kind of program feel free user it in your program. We will be using PHP as a scripting language that interepret in the webserver such as xamp, wamp, etc. It is widely use by modern website application to handle and protect user confidential information.

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_force, 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_force");
  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.         </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 - Drag And Drop Image Download</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <form method="POST" action="upload.php" enctype="multipart/form-data">
  18.                         <div class="form-inline">
  19.                                 <input type="file" class="form-control" name="file" required="required"/>
  20.                                 <button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button>
  21.                         </div>
  22.                 </form>
  23.                 <br />
  24.                 <table class="table table-bordered">
  25.                         <thead class="alert-info">
  26.                                 <tr>
  27.                                         <th>File</th>
  28.                                         <th>Action</th>
  29.                                 </tr>
  30.                         </thead>
  31.                         <tbody>
  32.                         <?php
  33.                                 require 'conn.php';
  34.                                
  35.                                 $query = mysqli_query($conn, "SELECT * FROM `file`") or die(mysqli_error());
  36.                                 while($fetch = mysqli_fetch_array($query)){
  37.                         ?>
  38.                                 <tr>
  39.                                         <td><?php echo $fetch['file']?></td>
  40.                                         <td><button type="button" class="btn btn-primary" onclick="download(this.name);" name="<?php echo $fetch['file_id']?>"><span class="glyphicon glyphicon-download"></span> Download</button></td>
  41.                                 </tr>
  42.                         <?php
  43.                                         }
  44.                         ?>
  45.                         </tbody>
  46.                 </table>
  47.         </div>
  48. <script type="text/javascript">
  49.         function download(id){
  50.                 alert("Download file forcefully!");
  51.                 window.location = 'download.php?file_id='+id;
  52.         }
  53. </script>      
  54. </body>
  55. </html>

Creating Upload Query

This code contains the php query of the application. This code will upload the file inputs 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($_POST['upload'])){
  5.                 $file_name = $_FILES['file']['name'];
  6.                 $file_temp = $_FILES['file']['tmp_name'];
  7.                 $file_size = $_FILES['file']['size'];
  8.                 $exp = explode(".", $file_name);
  9.                 $ext = end($exp);
  10.                 $name = date("Y-m-d h-i-s").".".$ext;
  11.                 $path = "uploads/".$name;
  12.                
  13.                 if($file_size > 5242880){
  14.                         echo "<script>alert('File too large')</script>";
  15.                         echo "<script>window.location='index.php'</script>";
  16.                 }else{
  17.                         if(move_uploaded_file($file_temp, $path)){
  18.                                 mysqli_query($conn, "INSERT INTO `file` VALUES('', '$name', '$path')") or die(mysqli_error());
  19.                                 header("location: index.php");
  20.                         }
  21.                 }
  22.         }
  23. ?>

Creating the Main Function

This code contains the main function of the application. This code will forcefully the download a file when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as download.php.
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if(ISSET($_REQUEST['file_id'])){
  5.                 $file = $_REQUEST['file_id'];
  6.                 $query = mysqli_query($conn, "SELECT * FROM `file` WHERE `file_id` = '$file'") or die(mysqli_error());
  7.                 $fetch = mysqli_fetch_array($query);
  8.                
  9.                 header("Content-Disposition: attachment; filename=".$fetch['file']);
  10.                 header("Content-Type: application/octet-stream;");
  11.                 readfile("uploads/".$fetch['file']);
  12.         }
  13. ?>
There you have it we successfully created Simple Force Download File 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