Bikin halaman upload & download
CREATE TABLE IF NOT EXISTS `upload` (
`id` int(5) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
`type` varchar(30) NOT NULL,
`size` int(11) NOT NULL,
`username` varchar(25) NOT NULL,
`email` varchar(50) NOT NULL,
`datetime` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
<?php
$host ="localhost";
$username = "root";
$password = "monkey";
$db_name = "tutorial";
$tbl_name = "upload";
mysql_connect("$host","$username","$password") or die ("Cannot connect to server");
mysql_select_db("$db_name")or die("cannot select database");
?>
<html>
<head><title>Form Upload</title></head>
<body>
<h1> Upload </h1>
<table width="600" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong> Upload your problem in file document here... </strong>
</td>
</tr>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#cccccc">
<tr>
<form enctype="multipart/form-data" method="POST" action="upload.php">
<td>
<table width="600" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">
<input type="hidden" name="MAX_FILE_SIZE" value="3000000"/> // valuenya itu dalam satuan kb
</td>
<td width="14"> </td>
<td width="357"> </td>
</tr>
<tr>
<td>Input your name</td>
<td>:</td>
<td><input name="user" type="text" id="user" size="25" maxlength="25" title="masukan nama anda"></td>
</tr>
<tr>
<td>Input your email</td>
<td>:</td>
<td><input name="email" type="text" id="email" size="25" maxlength="50" title="masukan email anda"></td>
</tr>
<tr>
<td>Browse Your File</td>
<td>:</td>
<td>
<input name="userfile" type="file"/></td>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><i>max size 3 mb, only for *.doc *.pdf</i>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" value="Upload"/>
</td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
<?php
include('connectup.php');
// setting nama folder tempat upload
$uploaddir = 'data/';
// membaca nama file yang diupload
$fileName = $_FILES['userfile']['name'];
// nama file temporary yang akan disimpan di server
$tmpName = $_FILES['userfile']['tmp_name'];
// membaca ukuran file yang diupload
$fileSize = $_FILES['userfile']['size'];
// membaca jenis file yang diupload
$fileType = $_FILES['userfile']['type'];
$username = $_POST['user'];
$email = $_POST['email'];
$datetime = date("y-m-d h:i:s");
$kode_user= $_POST['kode_user'];
$kode_server = $_POST['kode_server'];
// menyimpan properti atau informasi file ke tabel upload dalam db
// dengan terlebih dahulu mengecek ada tidaknya nama file dalam tabel
$query = "SELECT count(*) as jum FROM upload WHERE name = '$fileName'";
$hasil = mysql_query($query);
$data = mysql_fetch_array($hasil);
if ($data['jum'] > 0)
{
$query = "UPDATE upload SET size = '$fileSize' WHERE name = '$fileName'";
}
else $query = "INSERT INTO upload (name, size, type, username, email, datetime) VALUES ('$fileName', '$fileSize', '$fileType', '$username', '$email', '$datetime')";
mysql_query($query);
// menggabungkan nama folder dan nama file
$uploadfile = $uploaddir . $fileName;
// proses upload file ke folder 'data'
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "<script>alert('File has been uploaded');</script>";
echo "<meta http-equiv='refresh' content='0;URL=index.php'>";
} else { echo "file failed to upload";
}
?>
Gimane bro ?? mayan kan ?? nyahaha kita bisa download (ngambil data) sama ngapus file file yang pengen kita buang. bikin dulu ah form nya ya nih skripnya, simpennya dengan nama liat.php yaa
<html>
<head><title>Liat</title></head>
<body>
<h2><center>List data yang telah di upload</center><h2>
<table border='1' cellpadding='5' cellspacing='1' align='center' >
<tr>
<th>user</th>
<th>email</th>
<th>nama file</th>
<th>type</th>
<th>size</th>
<th>datetime</th>
<th> </th>
<th> </th>
</tr>
<?php
include'connectup.php';
$sql="SELECT * FROM upload";
$result=mysql_query($sql);
while ($isi=mysql_fetch_array($result))
{
$user = $isi['username'];
$email = $isi['email'];
$namafile = $isi['name'];
$type = $isi['type'];
$size = $isi['size'];
$datetime = $isi['datetime'];
echo "
<tr>
<td>$user</td>
<td>$email</td>
<td>$namafile</td>
<td>$type</td>
<td>$size</td>
<td>$datetime</td>
<td><a href='download.php?id=".$isi['id']."'>Download</a></td>
<td><a href='hapus.php?id=".$isi['id']."'>Delete</a></td>
</tr>";
}
?>
</table>
</body>
</html>
liat tulisan yang download.php sama hapus.php gak ?? itu skrip berfungsi buat ngedownload file sama buat ngehapus file, sekarang kita bikin yang download.php dulu ya, nih skripnya :
*nanti kalo ada error dalam link buat downloadnya coba tolong ditambahin ' ../ ' tanpa tanda kutip di tulisan warna orange sebelum tulisan data, jadi nanti tulisannya begini ../data/<?phpinclude'connectup.php';$id=$_GET['id'];$query="SELECT * FROM upload WHERE id='$id'";$result=mysql_query($query);if(!result){echo mysql_error(); exit;}
$row=mysql_fetch_array($result);
ob_start();
header("content-Transfer-Encoding:binary\n");
header("Content-Disposition: attachment; filename=".$row['name']);
header("Content-length:".$row['size']);
header("Content-type:".$row['type']);
ob_end_flush();
$fp = fopen("data/".$row['name'],'r');
$content = fread($fp, filesize('data/'.$row['name']));fclose($fp);echo $content;?>
udah kelar ?? lanjut lah buat bikin skrip hapus.php, nih skripnya monggo :
<?phpnanti juga kalo di skrip hapus ini ada masalah di unlink nya, coba di tambahin lagi ya ' ../ ' tanpa tanda kutip di tulisan warna orange sebelum tulisan data, jadi nanti tulisannya begini ../data/
include'connectadm.php';
$id = $_GET['id'];
$query="SELECT * FROM upload WHERE id='$id'";
$result =mysql_query($query);
$data = mysql_fetch_array($result);
$namaFile = $data['name'];
//query untuk menghapus
$query="DELETE FROM upload WHERE id='$id'";
mysql_query($query);
//menghapus file dalam folder sesuai namanyaunlink("data/".$namaFile);echo"<script>alert('File berhasil dihapus');</script>";
echo "<meta http-equiv='refresh' content='0;URL=liat.php'>";
?>