Breaking News
Loading...
Loading...
Tuesday, March 21, 2017

Membuat script upload file Php

March 21, 2017
Assalamualaikum

Langsung Coba Gan


<?php
include('config.php');
include('action_upload.php');
?>
<style type="text/css">
.table{
font:normal 12px Tahoma,verdana;
border:silver 1px solid;
width:350px;
}
.table tr td{
border-bottom:silver 1px solid;
border-right:silver 1px solid;
padding:0 5px 0 5px;
}
.table tr td.title{
font:bold 12px Tahoma,verdana;
background-color:#999999;
color:#000000;
}
input{
font:normal 12px Tahoma,verdana;
}
#eror{
width:345px;;
border:red 1px solid;
margin-left:auto;
margin-right:auto;
margin-bottom:5px;
padding:0 0 0 5px;
}
#msg{
width:345px;;
border:green 1px solid;
margin-left:auto;
margin-right:auto;
margin-bottom:5px;
padding:0 0 0 5px;
}
</style>
<form method="post" enctype="multipart/form-data" action="">
<table class="table" cellpadding="0" cellspacing="0" align="center">
<tr>
<td colspan="2" height="25" class="title">Form Upload File</td>
</tr>
<tr>
<td width="100">File</td>
<td><input type="file" name="data_upload" /></td>
</tr>
<tr>
<td width="100" valign="top">Keterangan</td>
<td><textarea name="keterangan" cols="30" rows="3"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="btnUpload" value="Upload" /></td>
</tr>
</table>
</form>



-------------------------------------------------------------------------------------------------



<?php
//Buat konfigurasi upload
//Folder tujuan upload file
$eror = false;
$folder = './upload/';
//type file yang bisa diupload
$file_type = array('jpg','jpeg','png','gif','bmp','doc','docx','xls','xlsx','sql');
//tukuran maximum file yang dapat diupload
$max_size = 1000000; // 1MB
if(isset($_POST['btnUpload'])){
//Mulai memorises data
$file_name = $_FILES['data_upload']['name'];
$file_size = $_FILES['data_upload']['size'];
//cari extensi file dengan menggunakan fungsi explode
$explode = explode('.',$file_name);
$extensi = $explode[count($explode)-1];

//check apakah type file sudah sesuai
if(!in_array($extensi,$file_type)){
$eror   = true;
$pesan .= '- Type file yang anda upload tidak sesuai<br />';
}
if($file_size > $max_size){
$eror   = true;
$pesan .= '- Ukuran file melebihi batas maximum<br />';
}
//check ukuran file apakah sudah sesuai

if($eror == true){
echo '<div id="eror">'.$pesan.'</div>';
}
else{
//mulai memproses upload file
if(move_uploaded_file($_FILES['data_upload']['tmp_name'], $folder.$file_name)){
//catat nama file ke database
$catat = mysql_query('insert into file_upload(Filename,Detail,Folder,DateUpload) values ("'.$file_name.'", "'.$_POST['keterangan'].'",
 "'.$folder.'", "'.date('Y-m-d H:i:s').'")');
echo '<div id="msg">Berhasil mengupload file '.$file_name.'</div>';
} else{
echo "Proses upload eror";
}
}
}
?>

0 comments:

Post a Comment

ANDA SOPAN KAMI SEGAN :)

 
Toggle Footer