FILE UPLOAD DAN DOWNLOAD PADA PHP MYSQL

beberapa fitur yang akan dibuat adalah:
  • User bisa Upload
  • User bisa Download
  • Pembatasan pada ektensi file yang bisa di Upload, hanya bisa Upload file dengan ekstensi doc, docx, xls, xlsx, ppt, pptx, pdf, rar, zip (bisa dirubah)
  • Pembatasan file size 1 MB (bisa dirubah)

Dan berikut ini adalah screenshot dari file-file yang akan dibuat


Langkah..

1. Buat Database dan tabel

CREATE TABLE `download` (
  `id` int(11) NOT NULL auto_increment,
  `tanggal_upload` date NOT NULL,
  `nama_file` varchar(100) NOT NULL,
  `tipe_file` varchar(10) NOT NULL,
  `ukuran_file` varchar(20) NOT NULL,
  `file` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)

);

2. config.php

<?php
//koneksi ke database
mysql_connect("localhost", "root", "root");
mysql_select_db("tutorial");

//fungsi untuk mengkonversi size file
function formatBytes($bytes, $precision = 2) {
    $units = array('B', 'KB', 'MB', 'GB', 'TB');

    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);

    $bytes /= pow(1024, $pow);

    return round($bytes, $precision) . ' ' . $units[$pow];
}
?>

3. index.php

<!DOCTYPE html>
<html>
<head>
<title>Simple Upload dan Download File</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<div id="container">
    <div id="header">
    <h1>Simple Upload dan Download File</h1>
        <span>Dibuat oleh Pino @tutorialweb.net</span>
        </div>

        <div id="menu">
        <a href="index.php" class="active">Home</a>
            <a href="upload.php">Upload</a>
            <a href="download.php">Download</a>
        </div>

        <div id="content">
        <h2>Home</h2>
            <p>Selamat Datang!</p>
            <p>Web Simple Download dan Upload File ini dibuat oleh <strong>TUTORIALWEB.NET</strong>. Anda bisa mempublikasikan ulang, atau merubah Source Code web ini. Jangan lupa untuk mengunjungi <a href="http://www.tutorialweb.net/" target="_blank">TUTORIALWEB.NET</a> untuk tutorial-tutorial pemrograman lainnya.</p>
        </div>
    </div>

</body>
</html>

4. download.php

<!DOCTYPE html>
<html>
<head>
<title>Simple Upload dan Download File</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<div id="container">
    <div id="header">
    <h1>Simple Upload dan Download File</h1>
        <span>Dibuat oleh Pino @tutorialweb.net</span>
        </div>

        <div id="menu">
        <a href="index.php">Home</a>
            <a href="upload.php">Upload</a>
            <a href="download.php" class="active">Download</a>
        </div>

        <div id="content">
        <h2>Download</h2>
            <p>Silahkan download file yang sudah di Upload di website ini. Untuk men-Download Anda bisa mengklik Judul file yang di inginkan.</p>

            <p>
            <table class="table" width="100%" cellpadding="3" cellspacing="0">
            <tr>
                <th width="30">No.</th>
                    <th width="80">Tgl. Upload</th>
                    <th>Nama File</th>
                    <th width="70">Tipe</th>
                    <th width="70">Ukuran</th>
                </tr>
                <?php
include('config.php');
$sql = mysql_query("SELECT * FROM download ORDER BY id DESC");
if(mysql_num_rows($sql) > 0){
$no = 1;
while($data = mysql_fetch_assoc($sql)){
echo '
<tr bgcolor="#fff">
<td align="center">'.$no.'</td>
<td align="center">'.$data['tanggal_upload'].'</td>
<td><a href="'.$data['file'].'">'.$data['nama_file'].'</a></td>
<td align="center">'.$data['tipe_file'].'</td>
<td align="center">'.formatBytes($data['ukuran_file']).'</td>
</tr>
';
$no++;
}
}else{
echo '
<tr bgcolor="#fff">
<td align="center" colspan="4" align="center">Tidak ada data!</td>
</tr>
';
}
?>
            </table>
            </p>
        </div>
    </div>

</body>
</html>

5. upload.php

<!DOCTYPE html>
<html>
<head>
<title>Simple Upload dan Download File</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<div id="container">
    <div id="header">
    <h1>Simple Upload dan Download File</h1>
        <span>Dibuat oleh Pino @tutorialweb.net</span>
        </div>

        <div id="menu">
        <a href="index.php">Home</a>
            <a href="upload.php" class="active">Upload</a>
            <a href="download.php">Download</a>
        </div>

        <div id="content">
        <h2>Upload</h2>
            <p>Upload file Anda dengan melengkapi form di bawah ini. File yang bisa di Upload hanya file dengan ekstensi <b>.doc, .docx, .xls, .xlsx, .ppt, .pptx, .pdf, .rar, .zip</b> dan besar file (file size) maksimal hanya 1 MB.</p>

            <?php
include('config.php');
if($_POST['upload']){
$allowed_ext = array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'rar', 'zip');
$file_name = $_FILES['file']['name'];
$file_ext = strtolower(end(explode('.', $file_name)));
$file_size = $_FILES['file']['size'];
$file_tmp = $_FILES['file']['tmp_name'];

$nama = $_POST['nama'];
$tgl = date("Y-m-d");

if(in_array($file_ext, $allowed_ext) === true){
if($file_size < 1044070){
$lokasi = 'files/'.$nama.'.'.$file_ext;
move_uploaded_file($file_tmp, $lokasi);
$in = mysql_query("INSERT INTO download VALUES(NULL, '$tgl', '$nama', '$file_ext', '$file_size', '$lokasi')");
if($in){
echo '<div class="ok">SUCCESS: File berhasil di Upload!</div>';
}else{
echo '<div class="error">ERROR: Gagal upload file!</div>';
}
}else{
echo '<div class="error">ERROR: Besar ukuran file (file size) maksimal 1 Mb!</div>';
}
}else{
echo '<div class="error">ERROR: Ekstensi file tidak di izinkan!</div>';
}
}
?>

            <p>
            <form action="" method="post" enctype="multipart/form-data">
            <table width="100%" align="center" border="0" bgcolor="#eee" cellpadding="2" cellspacing="0">
            <tr>
                <td width="40%" align="right"><b>Nama File</b></td><td><b>:</b></td><td><input type="text" name="nama" size="40" required /></td>
                </tr>
                <tr>
                <td width="40%" align="right"><b>Pilih File</b></td><td><b>:</b></td><td><input type="file" name="file" required /></td>
                </tr>
                <tr>
                <td>&nbsp;</td><td>&nbsp;</td><td><input type="submit" name="upload" value="Upload" /></td>
                </tr>
            </table>
            </form>
            </p>
        </div>
    </div>

</body>
</html>

6. style.css

@charset "utf-8";
/* CSS Document */

body {
font-family:Tahoma, Geneva, sans-serif;
font-size:12px;
background-color:#eee;
margin:0;
padding:0;
}

h1, h2, h3 {
margin:0;
padding:0;
}

#container {
width:500px;
margin:20px auto;
padding:10px;
background-color:#fff;
box-shadow:0px 0px 3px #000;
}

#header {
text-align:center;
}

#menu {
text-align:center;
margin:15px 0px;
border-top:1px solid #CCC;
border-bottom:1px solid #CCC;
}

#menu a {
display:inline-block;
padding:5px 10px;
text-decoration:none;
color:#000;
font-weight:bold;
}

#menu a:hover {
background-color:#CCC;
}

#menu a.active {
background-color:#CCC;
}

.table, th, td {
border-collapse:collapse;
border:1px solid #ccc;
}

.table th {
background-color:#CCC;
}

.error {
border:1px solid #FF8080;
background-color:#FFCECE;
padding:3px;
margin:5px 0px;
text-align:center;
}

.ok {
border:1px solid #80FF80;
background-color:#CFC;
padding:3px;
margin:5px 0px;
text-align:center;
}


TUGAS :
1. Praktekkan Program diatas..
2. Jalankan Program dan buatlah Video
3. Upload diYoutube
4. Kirimkan ALamat youtube ke email : harrysupandi@gmail.com

Selamat Mengerjakan

Tidak ada komentar:

Posting Komentar