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.
Creating the database Connection
This is where the database connection, just simple copy/paste the provided code below
and save it as 'connection.php'.
<?php
$conn = new mysqli('localhost', 'root', '', 'system');
if($conn->connect_error){
die("Fatal Error: Can't connect to database: ". $conn->connect_error); }
?>
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.
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<a class="navbar-brand" href="https://sourcecdester.com">Sourcecodester</a>
</div>
</nav>
<div class="col-md-3"></div>
<div class="col-md-6 well">
<h3 class="text-primary">PHP - Upload and Download File Application</h3>
<hr style="border-top:1px dottec #ccc;">
<form class="form-inline" method="POST" action="upload.php" enctype="multipart/form-data">
<input class="form-control" type="file" name="upload"/>
<button type="submit" class="btn btn-success form-control" name="submit"><span class="glyphicon glyphicon-upload"></span> Upload</button>
</form>
<br /><br />
<table class="table table-bordered">
<thead class="alert-warning">
<tr>
<th>Name</th>
<th>Action</th>
</tr>
</thead>
<tbody class="alert-success">
<?php
require 'connection.php';
while($fetch = $row->fetch_array()){
?>
<tr>
<?php
$name = explode('/', $fetch['file']); ?>
<td><?php echo $fetch['name']?></td>
<td><a href="download.php?file=<?php echo $name[1]?>" class="btn btn-primary"><span class="glyphicon glyphicon-download"></span> Download</a></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</body>
</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.
<?php
require_once 'connection.php';
if(ISSET($_POST['submit'])){ if($_FILES['upload']['name'] != "") {
$file = $_FILES['upload'];
$file_name = $file['name'];
$file_temp = $file['tmp_name'];
$path = "files/".$file_name;
$conn->query("INSERT INTO `file` VALUES('', '$name[0]', '$path')") or
die(mysqli_error());
}else{
echo "<script>alert('Required Field!')</script>";
echo "<script>window.location='index.php'</script>";
}
}
?>
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.
<?php
if(ISSET($_REQUEST['file'])){ $file = $_REQUEST['file'];
header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Type: application/octet-stream;"); header("Content-Transfer-Encoding: binary"); }
?>
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!!!