<?
mysql_connect('localhost','userdb','passw');
mysql_select_db('db_name');
if($_POST['upload'])
{
$tanggal = date("Y-m-d");
// membaca keterangan
$ket = $_POST['ket'];
// membaca nama file
$fileName = $_FILES['userfile']['name'];
// membaca nama file temporary
$tmpName = $_FILES['userfile']['tmp_name'];
// membaca size file
$fileSize = $_FILES['userfile']['size'];
// membaca tipe file
$fileType = $_FILES['userfile']['type'];
// membaca isi file yang diupload
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
// query SQL untuk menyimpan isi file dan informasi lainnya ke database
$query = "INSERT INTO upload (name, size, type, content, dateupload, keterangan)
VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$tanggal', '$ket')";
$hasil = mysql_query($query);
// konfirmasi proses upload
if ($hasil) echo "<p>File ".$fileName." telah terupload</p>";
else echo "<p>File ".$fileName." gagal diupload</p>";
}
if($_GET['stat']=="download")
{
$id = $_GET['id'];
// query untuk membaca data file berdasarkan ID
$query = "SELECT * FROM upload WHERE id = $id";
$hasil = mysql_query($query);
$data = mysql_fetch_array($hasil);
// mengenerate proses download file
header("Content-Disposition: attachment; filename=".$data['name']);
header("Content-length: ".$data['size']);
header("Content-type: ".$data['type']);
echo $data['content'];
}
?>
<h1>Upload File</h1>
<form method="post" enctype="multipart/form-data" action="./">
Pilih File <br>
<input name="userfile" type="file"><br>
Keterangan:<br>
<textarea name="keterangan"></textarea><br>
<input name="upload" type="submit" value="Upload">
</form>
<?php
echo "<h2>Daftar File Upload</h2>";
// query untuk membaca semua file yang terupload
$query = "SELECT * FROM upload";
$hasil = mysql_query($query);
// menampilkan daftar file
echo "<table border='1'>";
echo "<tr><th>ID</th><th>Nama File</th><th>Tanggal Upload</th><th>Keterangan</th><th>Action</th></tr>";
while ($data = mysql_fetch_array($hasil))
{
echo "<tr><td>".$data['id']."</td><td><a href='./?stat=download&id=".$data['id']."'>".$data['name']."</a></td><td>".$data['dateupload']."</td><td>".$data['ket']."</td></tr>";
}
echo "</table>";
?>