Sabtu, 02 April 2016

Upload & Download file dengan PHP MySQL

 Upload & Download file dengan PHP MySQL
 Asslamu'alaikum Wr.Wb

Hari ini saya akan sharing tentang cara Upload & Download file dengan PHP MySQL...

Nah langsung saja

1. Membuat database 

 DROP TABLE IF EXISTS `download`;
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`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
2. Membuat  file config.php kemudian isikan

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

//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. membuat file index.php dan isikan skrip berikut :

<!DOCTYPE html>
<html>
<head>
    <title>Upload dan Download File</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body style="background-color: white">
<center>
<img src="files/minions.jpg" style="width: 500px; height: 250px;">
</center>
    <div id="container">
        <div id="header">
            <h1>Upload dan Download File</h1>
            <span>Dibuat oleh <blink>Luthfi Zakariya</blink></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>Luthfi Zakariya</strong>. Anda bisa mempublikasikan ulang, atau merubah Source Code web ini. Jangan lupa untuk mengunjungi <a href="http://lutfizakariya.blogspot.com/" target="_blank">BLOGGER</a> untuk tutorial-tutorial pemrograman lainnya.</p>
        </div>
    </div>
    <div id="link-left">
    <a href="#">Home</a>
</body>
</html>


4. Buat file downloadnya, simpan dengan nama download.php

<!DOCTYPE html>
<html>
<head>
    <title>Simple Upload dan Download File</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body style="background-color: white">
<center>
<img src="files/minions_download.jpg" style="width: 500px; height: 250px;">
</center>
    <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. Membuat file upload.php

<!DOCTYPE html>
<html>
<head>
    <title>Simple Upload dan Download File</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body style="background-color: white">
<center>
<img src="files/minions_upload.jpg" style="float: left; width: 250px; height: 250px;">
</center>
    <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. buat style.css untuk tampilanya biar sedikit bagus

@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;
    border-radius: 50px;
    padding:10px;
    background-color: lightblue;
    box-shadow:0px 10px 50px yellow;
}

#header {
    text-align:center;
}

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

    #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;


SEKIAN dari SAYA

0 komentar:

Posting Komentar

Twitter Delicious Facebook Digg Stumbleupon Favorites More