PHP - Upload and Download File Application

In this tutorial we will create a Upload and Download File Application 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.

Before we 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.

Creating Database

This is where we store all the data we process throug HTML forms. To create a database open any kind of database base server that you have(wamp, xamp, etc..). Then create database and name it system. After that click SQL then copy/paste the code below then click GO.
  1. CREATE TABLE `file` (
  2. `file_id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(200) NOT NULL,
  4. `file` varchar(500) NOT NULL,
  5. PRIMARY KEY (`file_id`)

Creating the database Connection

This is where the database connection, just simple copy/paste the provided code below and save it as 'connection.php'.
  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'system');
  3. if($conn->connect_error){
  4. die("Fatal Error: Can't connect to database: ". $conn->connect_error);
  5. }
  6. ?>

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. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecdester.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 - Upload and Download File Application</h3>
  16. <hr style="border-top:1px dottec #ccc;">
  17. <form class="form-inline" method="POST" action="upload.php" enctype="multipart/form-data">
  18. <input class="form-control" type="file" name="upload"/>
  19. <button type="submit" class="btn btn-success form-control" name="submit"><span class="glyphicon glyphicon-upload"></span> Upload</button>
  20. </form>
  21. <br /><br />
  22. <table class="table table-bordered">
  23. <thead class="alert-warning">
  24. <tr>
  25. <th>Name</th>
  26. <th>Action</th>
  27. </tr>
  28. </thead>
  29. <tbody class="alert-success">
  30. <?php
  31. require 'connection.php';
  32. $row = $conn->query("SELECT * FROM `file`") or die(mysqli_error());
  33. while($fetch = $row->fetch_array()){
  34. ?>
  35. <tr>
  36. <?php
  37. $name = explode('/', $fetch['file']);
  38. ?>
  39. <td><?php echo $fetch['name']?></td>
  40. <td><a href="download.php?file=<?php echo $name[1]?>" class="btn btn-primary"><span class="glyphicon glyphicon-download"></span> Download</a></td>
  41. </tr>
  42. <?php
  43. }
  44. ?>
  45. </tbody>
  46. </table>
  47. </div>
  48. </body>
  49. </html>

Creating the Upload

This code contains the specific script for the upload. This will process the file that have been upload then will save it to a designated directory. To do that write these block of codes inside the Text editor and call it as upload.php.
  1. <?php
  2. require_once 'connection.php';
  3.  
  4. if(ISSET($_POST['submit'])){
  5. if($_FILES['upload']['name'] != "") {
  6. $file = $_FILES['upload'];
  7.  
  8. $file_name = $file['name'];
  9. $file_temp = $file['tmp_name'];
  10. $name = explode('.', $file_name);
  11. $path = "files/".$file_name;
  12.  
  13. $conn->query("INSERT INTO `file` VALUES('', '$name[0]', '$path')") or die(mysqli_error());
  14.  
  15. move_uploaded_file($file_temp, $path);
  16. header("location:index.php");
  17.  
  18. }else{
  19. echo "<script>alert('Required Field!')</script>";
  20. echo "<script>window.location='index.php'</script>";
  21. }
  22. }
  23. ?>

Creating the Download

This code contains the function for the download script. This code will force to download a specific file when a button is clicked. To do this just write these block of 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("Cache-Control: public");
  6. header("Content-Description: File Transfer");
  7. header("Content-Disposition: attachment; filename=".basename($file));
  8. header("Content-Type: application/octet-stream;");
  9. header("Content-Transfer-Encoding: binary");
  10. readfile("files/".$file);
  11. }
  12. ?>
There you have it we successfully created a Upload and Download File Application 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

Add new comment