-
Notifications
You must be signed in to change notification settings - Fork 2
/
upload_referensi.php
180 lines (162 loc) · 9.08 KB
/
upload_referensi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
//cek session
if(empty($_SESSION['admin'])){
$_SESSION['err'] = '<center>Anda harus login terlebih dahulu!</center>';
header("Location: ./");
die();
} else {
if($_SESSION['admin'] != 1 AND $_SESSION['admin'] != 2){
echo '<script language="javascript">
window.alert("ERROR! Anda tidak memiliki hak akses untuk membuka halaman ini");
window.location.href="./logout.php";
</script>';
} else {
//proses upload file
if(isset($_POST['submit'])){
$file = $_FILES['file']['tmp_name'];
if($file == ""){
$_SESSION['errEmpty'] = 'ERROR! Form File tidak boleh kosong';
header("Location: ./admin.php?page=ref&act=imp");
die();
} else {
$x = explode('.', $_FILES['file']['name']);
$eks = strtolower(end($x));
if($eks == 'csv'){
//upload file
if(is_uploaded_file($file)){
$_SESSION['succUpload'] = 'SUKSES! Data berhasil diimport';
} else {
$_SESSION['errUpload'] = 'ERROR! Proses upload data gagal';
header("Location: ./admin.php?page=ref&act=imp"); die();
}
//membuka file csv
$handle = fopen($file, "r");
$id_user = $_SESSION['id_user'];
//parsing file csv
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE){
//insert data ke dalam database
$query = mysqli_query($config, "INSERT into tbl_klasifikasi(id_klasifikasi,kode,nama,uraian,id_user) values(null,'$data[1]','$data[2]','$data[3]','$id_user')");
}
fclose($handle);
header("Location: ./admin.php?page=ref");
die();
} else {
$_SESSION['errFormat'] = 'ERROR! Format file yang diperbolehkan hanya *.CSV';
header("Location: ./admin.php?page=ref&act=imp");
die();
}
}
}
echo '
<!-- Row Start -->
<div class="row">
<!-- Secondary Nav START -->
<div class="col s12">
<div class="z-depth-1">
<nav class="secondary-nav">
<div class="nav-wrapper blue-grey darken-1">
<div class="col m12">
<ul class="left">
<li class="waves-effect waves-light"><a href="?page=ref&act=imp" class="judul"><i class="material-icons">bookmark</i> Import Referensi Surat</a></li>
<li class="waves-effect waves-light"><a href="?page=ref"><i class="material-icons">arrow_back</i> Kembali</a></li>
</ul>
</div>
</div>
</nav>
</div>
</div>
<!-- Secondary Nav END -->
</div>
<!-- Row END -->';
if(isset($_SESSION['errFormat'])){
$errFormat = $_SESSION['errFormat'];
echo '<div id="alert-message" class="row">
<div class="col m12">
<div class="card red lighten-5">
<div class="card-content notif">
<span class="card-title red-text"><i class="material-icons md-36">clear</i> '.$errFormat.'</span>
</div>
</div>
</div>
</div>';
unset($_SESSION['errFormat']);
}
if(isset($_SESSION['errUpload'])){
$errUpload = $_SESSION['errUpload'];
echo '<div id="alert-message" class="row">
<div class="col m12">
<div class="card red lighten-5">
<div class="card-content notif">
<span class="card-title red-text"><i class="material-icons md-36">clear</i> '.$errUpload.'</span>
</div>
</div>
</div>
</div>';
unset($_SESSION['errUpload']);
}
if(isset($_SESSION['errEmpty'])){
$errEmpty = $_SESSION['errEmpty'];
echo '<div id="alert-message" class="row">
<div class="col m12">
<div class="card red lighten-5">
<div class="card-content notif">
<span class="card-title red-text"><i class="material-icons md-36">clear</i> '.$errEmpty.'</span>
</div>
</div>
</div>
</div>';
unset($_SESSION['errEmpty']);
}
echo '
<!-- Row form Start -->
<div class="row">
<div class="col m12">
<div class="card">
<div class="card-content">
<span class="card-title black-text">Import Referensi Kode Klasifikasi Surat</span>
<p class="kata">Silakan pilih file referensi kode klasifikasi berformat *.csv (file excel) lalu klik tombol <strong>"Import"</strong> untuk melakukan import file. Contoh format file csv bisa di download melalui link dibawah ini.</p><br/>';
// download file contoh format csv
if(isset($_REQUEST['download'])){
$dir = "./asset/";
$file = $dir."contoh_format.csv";
if(file_exists($file)){
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="contoh_format.csv"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: private');
header('Pragma: private');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
} echo '
<p>
<form method="post" enctype="multipart/form-data" >
<a href="?page=ref&act=imp&download" name="download" class="waves-effect waves-light blue-text"><i class="material-icons">file_download</i> <strong>DOWNLOAD CONTOH FORMAT FILE CSV</strong></a>
</form>
</p>
</div>
<div class="card-action">
<form method="post" enctype="multipart/form-data">
<div class="file-field input-field col m6">
<div class="btn light-green darken-1">
<span>File</span>
<input type="file" name="file" accept=".csv" required>
</div>
<div class="file-path-wrapper">
<input class="file-path validate" placeholder="Upload file csv referensi kode klasifikasi" type="text">
</div>
</div>
<button type="submit" class="btn-large blue waves-effect waves-light" name="submit">IMPORT <i class="material-icons">file_upload</i></button>
</form>
</div>
</div>
</div>
</div>';
}
}
?>