Membuat Form Upload dan Download File dengan PHP dan MySQLi
-Upload File-
<html>
<title>Form Upload</title>
<body>
<form enctype="multipart/form-data" method="POST" action="hasil_upload.php">
File yang di upload : <input type="file" name="fupload"><br>
Deskripsi File : <br>
<textarea name="deskripsi" rows="8" cols="40"></textarea><br>
<input type=submit value=Upload>
</form>
<title>Aplikasi Download</title>
<body>
<?php
$konek = mysqli_connect("localhost","root","","upload");
$query = "SELECT * FROM upload ORDER BY id_upload DESC";
$hasil = mysqli_query($konek, $query);
while ($r = mysqli_fetch_array($hasil)){
echo "Nama File : <b>$r[nama_file]</b> <br>";
echo "Deskripsi : $r[deskripsi] <br>";
echo "<a href=\"simpan.php?file=$r[nama_file]\">Download File</a><hr><br>";
}
?>
script simpan.php
// Tentukan folder file yang boleh di download
$folder = "files/";
// Lalu cek menggunakan fungsi file_exist
if (!file_exists($folder.$_GET['file'])) {
echo "<h1>Access forbidden!</h1>
<p> Anda tidak diperbolehkan mendownload file ini.</p>";
exit;
}
// Apabila mendownload file di folder files
else {
header("Content-Type: octet/stream");
header("Content-Disposition: attachment;
filename=\"".$_GET['file']."\"");
$fp = fopen($folder.$_GET['file'], "r");
$data = fread($fp, filesize($folder.$_GET['file']));
fclose($fp);
print $data;
}
?>
ok, sekian tutorial singkat ini, semoga bisa berguna
0 komentar:
Post a Comment