Categories
PHP

Membuat Aplikasi Simple PHP Download

php download

Selamat pagi kali ini kami akan mencoba membahasa bagaimana membuat Aplikasi simple PHP Download. Scrip ini akan secara paksa mengunduh daftar file yang diunggah ketika pengguna mengklik tombol unduh. Kode menggunakan header PHP untuk mengatur konten file di server database, dan untuk dapat memaksa mengunduh file.

Langsung saja mri kit amasuk kedalam pembahasan inti.

1. Buat database dengan nama db_force kemudian masukkan struktur database dibawah ini.

-- phpMyAdmin SQL Dump
-- version 4.8.3
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Mar 29, 2019 at 03:30 PM
-- Server version: 10.1.37-MariaDB
-- PHP Version: 7.2.12

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `db_force`
--

-- --------------------------------------------------------

--
-- Table structure for table `file`
--

CREATE TABLE `file` (
  `file_id` int(11) NOT NULL,
  `file` text NOT NULL,
  `location` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `file`
--

INSERT INTO `file` (`file_id`, `file`, `location`) VALUES
(1, '2019-03-29 03-29-29.jpeg', 'uploads/2019-03-29 03-29-29.jpeg');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `file`
--
ALTER TABLE `file`
  ADD PRIMARY KEY (`file_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `file`
--
ALTER TABLE `file`
  MODIFY `file_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

2. Buat file dengan nama conn.php file ini sebagai koneksi database.

<?php
	$conn = mysqli_connect("localhost", "root", "", "db_force");
	
	if(!$conn){
		die("Error: Failed to connect to database!");
	}
?>

3. Buat file dengan nama index.php file ini sebagai form.

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
		<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
	</head>
<body>
	<nav class="navbar navbar-default">
		<div class="container-fluid">
			<a class="navbar-brand" href="https://jasawebsite.ooo">Jasawebsite</a>
		</div>
	</nav>
	<div class="col-md-3"></div>	
	<div class="col-md-6 well">
		<h3 class="text-primary">Drag And Drop Image Download</h3>
		<hr style="border-top:1px dotted #ccc;"/>
		<form method="POST" action="upload.php" enctype="multipart/form-data">
			<div class="form-inline">
				<input type="file" class="form-control" name="file" required="required"/>
				<button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button>
			</div>
		</form>
		<br />
		<table class="table table-bordered">
			<thead class="alert-info">
				<tr>
					<th>File</th>
					<th>Action</th>
				</tr>
			</thead>
			<tbody>
			<?php
				require 'conn.php';
				
				$query = mysqli_query($conn, "SELECT * FROM `file`") or die(mysqli_error());
				while($fetch = mysqli_fetch_array($query)){
			?>
				<tr>
					<td><?php echo $fetch['file']?></td>
					<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>
				</tr>
			<?php
					}
			?>
			</tbody>
		</table>
	</div>
<script type="text/javascript">
	function download(id){
		alert("Apakah anda ingin Download File?");
		window.location = 'download.php?file_id='+id;
	}
</script>	
</body>	
</html>

4. Buat file dengan nama upoad.php script pada file ini akan mengunggah input file ke server database.

<?php
	require_once 'conn.php';
	
	if(ISSET($_POST['upload'])){
		$file_name = $_FILES['file']['name'];
		$file_temp = $_FILES['file']['tmp_name'];
		$file_size = $_FILES['file']['size'];
		$exp = explode(".", $file_name);
		$ext = end($exp);
		$name = date("Y-m-d h-i-s").".".$ext;
		$path = "uploads/".$name;
		
		if($file_size > 5242880){
			echo "<script>alert('File too large')</script>";
			echo "<script>window.location='index.php'</script>";
		}else{
			if(move_uploaded_file($file_temp, $path)){
				mysqli_query($conn, "INSERT INTO `file` VALUES('', '$name', '$path')") or die(mysqli_error());
				header("location: index.php");
			}
		}
	}
?>

5. Buat file dengan nama download.php script ini akan secara paksa mengunduh file ketika button diklik.

<?php
	require_once 'conn.php';
	
	if(ISSET($_REQUEST['file_id'])){
		$file = $_REQUEST['file_id'];
		$query = mysqli_query($conn, "SELECT * FROM `file` WHERE `file_id` = '$file'") or die(mysqli_error());
		$fetch = mysqli_fetch_array($query);
		
		header("Content-Disposition: attachment; filename=".$fetch['file']);
		header("Content-Type: application/octet-stream;");
		readfile("uploads/".$fetch['file']);
	}
?>

Kalian bisa mendowload filenya langsung dengan menekan button dibawah ini.

Sekian share kali ini semoga apa yang kami share bermanfaat bagi anda, silahakn tinggalkan komentar jika kalian belum begitu paham dan mulai berdiskusi dengan team kami.

Leave a Reply

Your email address will not be published. Required fields are marked *