Anda di halaman 1dari 4

Sebenarnya dalam upload file sendiri terdapat dua jenis yaitu menyimpan file ke direktori

kita dan yang kedua adalah menyimpan file ke database MySQL. Kali ini saya akan
menerangkan bagaimana caranya upload dan download file dan menyimpannya ke direktori
kita.

Langkah pertama kita akan membuat table yang berfungsi untuk menyimpan data dari file
yang akan di upload. Kenapa data dari file tersebut harus disimpan? tujuannya adalah agar
memudahkan dalam melakukan download.

Buatlah tabel dengan nama upload

CREATE TABLE `upload` (


`fileid` int(11) NOT NULL auto_increment,
`filename` varchar(30),
`type` varchar(40),
`size` int(11),
`path` varchar(20),
PRIMARY KEY (`fileid`)
);

Selanjutnya kita akan membuat file database.php untuk koneksi ke database MySQL

<?php
$db_hostname=localhost;
$db_username=root; // username anda
$db_password=; // password anda
$db_name=latihan; // nama database anda

function connect_db() {
global $db_hostname, $db_username, $db_password, $db_name;
$conn = mysql_connect($db_hostname, $db_username, $db_password) or die (`Sorry cannot
connect to the database because: ` . mysql_error());;
mysql_select_db($db_name);
}
?>

Kemudian kita buat form untuk upload file. Simpan file tersebut dengan nama
form_upload.php

<html>
<head><title>Upload</title></head>
<body>
<font align=center><h2>Form Upload</h2></font>
<form action=upload.php method=post enctype=multipart/form-data
name=uploadform>
<table align=center>
<tr><td>File</td><td><input name=userfile type=file></td></tr>
<tr><td></td><td><input name=tombol type=submit value=Upload></td></tr>
</table>
</form>
</body>
</html>

Selanjutnya buatlah file upload.php untuk menyimpan data file ke tabel upload. Di dalam file
upload.php terdapat variable $uploadDir, variable ini berisi path / folder yang berfungsi untuk
menyimpan file yang telah di upload. Untuk itu ubah hak akses folder tersebut agar bisa di isi
file.

<?php
require_once(database.php); //memanggil file database.php

connect_db(); // memanggil fungsi connect_db yang ada di file database.php


if($_POST[tombol])
{
$fileName = $_FILES[`userfile`][`name`];
$tmpName = $_FILES[`userfile`][`tmp_name`];
$fileSize = $_FILES[`userfile`][`size`];
$fileType = $_FILES[`userfile`][`type`];

$uploadDir = `C:/apache2triad/htdocs/upload/`;
$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);


if (!$result) {
$error_message=Error uploading file;
include(admin_error.php);
}

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = INSERT INTO upload(filename,type,size,path) VALUES (`$fileName`,


`$fileType`, `$fileSize`, `$fileName`);
$result=mysql_query($query);
if($result){
echo upload file berhasil dilakukan <br> <a href=`daftar_file.php`>Daftar File</a>;
}else{
echo <br>File can`t uploaded<br>;
}
}
?>

Selanjutnya kita akan membuat file dengan nama daftar_file.php. File ini digunakan untuk
menampilkan data yang di inputkan oleh user.

<html>
<head><title>Daftar File</title></head>
<body>
<?php
require_once(database.php);
connect_db();

$query=mysql_query(SELECT * FROM upload);


$row=mysql_fetch_row($query);
if(!$row)
echo tabel upload kosong;
else
{
echo <h2>Daftar File</h2>;
echo <table border=1>;
echo <tr>;
echo <td>Nama File</td>;
echo <td>Type</td>;
echo <td>Size</td>;
echo <td>Download</td>;
echo </tr>;

do{
list($fileid,$filename,$type,$size,$path)=$row;
echo <tr>;
echo <td>$filename</td>;
echo <td>$type</td>;
echo <td>$size</td>;
echo <td><a href=download.php?fileid=$fileid>Download</a></td>;
echo </tr>;
}while($row=mysql_fetch_row($query));
}

?>
</body>
</html>

Selanjutnya kita akan membuat file download.php untuk melakukan download.

<?php
require_once("database.php");
connect_db();
if(isset($_GET[fileid]))
{
$id = $_GET[fileid];
$query = "SELECT * FROM upload WHERE fileid = $fileid";
$result = mysql_query($query) or die(Error, query failed);
list($fileid,$filename,$type,$size,$path)=mysql_fetch_array($result);

header("Content-Disposition: attachment; filename=$filename");


header("Content-length: $size");
header("Content-type: $type");
readfile("upload/$path");
}
?>

Selanjutkan kita akan membuat file delete.php untuk menghapus file

<?
require_once("database.php");
connect_db();
if(isset($_GET[fileid]))
{
$id = $_GET[fileid];
$query = "SELECT * FROM upload WHERE fileid = $fileid";
$result = mysql_query($query) or die(Error, query failed);
list($fileid,$filename,$type,$size,$path)=mysql_fetch_array($result);
unlink("upload/$path");
mysql_query("delete from upload where fileid=$fileid");
header("location:./daftar_file.php");
}
?>

Anda mungkin juga menyukai